ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i am looking for a systematic and unified approach for managing log messages from c programs running on linux. This may take a syslog like approach. Are there any syslog libraries for c and any sample codes to use them ? I came across one in python http://docs.python.org/library/logging.html. But not sure if a wrapper is available for this library to be called from within a c program.
Any clues and directions will be highly appreciated!
Thanks for the help. I decided to first look at syslog library and found that i can actually do a syslog() in my c program. So, i picked up a quick example as:
syslog (LOG_MAKEPRI(LOG_LOCAL1, LOG_NOTICE), "Program started by User %d", getuid ());
syslog (LOG_INFO, "A tree falls in a forest");
closelog ();
return 0;
}
My intention here was to send all log messages going out of this program to a separate log file in /var/log/testProg.log. However, what i found was that all messages were going to /var/log/messages and /var/log/syslog. I figured out that i have to specify the destination for logs coming out of LOC_LOCAL1 facility in the /etc/syslog.conf. So, i did a little modification in the syslog and added the line:
local1.* -/var/log/testProg.log
But still no success and the messages still go to /var/log/messages and /var/log/syslog.
Any clues on where i am making the mistake.
Re the python wrapper, did u mean that there does exist a wrapper in python for c? Any links?
Re the python wrapper, did u mean that there does exist a wrapper in python for c? Any links?
The python provided thing is actually a wrapper...it is not the bare one. On *nix OS, the bare logging mechanism is provided by a library which is written in C. So, jiml8 mean to say that, when you are programming in python, you are really using a wrapper, but when you are programming in C, don't look for a wrapper, directly use the bare one provided by native library as you have done in your example...
If you want to send your log to a different file, it should be simple as you write to any other file,
1. write a function that uses fprintf to write to your own log file.
2. write a macro for that function.
3. use that macro wherever you want to have a log.
The python provided thing is actually a wrapper...it is not the bare one. On *nix OS, the bare logging mechanism is provided by a library which is written in C. So, jiml8 mean to say that, when you are programming in python, you are really using a wrapper, but when you are programming in C, don't look for a wrapper, directly use the bare one provided by native library as you have done in your example...
If you want to send your log to a different file, it should be simple as you write to any other file,
1. write a function that uses fprintf to write to your own log file.
2. write a macro for that function.
3. use that macro wherever you want to have a log.
Hope this make clear.
Srinivas
Thanks Srinivas for making the point clear. I could take the second approach u mentioned but with that i would have to do all by myself, in a way reinvent the logging whereas it is already pretty cool in python and c with standard logging, network logging and rotation functionalities.
ok guys, i found a way around. Spent the whole day with figuring out why the logs from syslog() function in c are not going to their separate files, i gave up!
So, the solution i discovered and that works for me is to use the Python logging and call python from C. A simple example is:
This sends log messages coming out of this program to myapp.log in a standard way.
I am planning to create a simple header file that will take care of initializing python etc., and provides functions that take log-file, log-level and log-message as arguments and take care of logging with rotation etc.
Thanks all for your help.
Last edited by middlewhere; 10-08-2009 at 12:45 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.