Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
10-06-2009, 05:59 AM
|
#1
|
LQ Newbie
Registered: Oct 2009
Posts: 6
Rep:
|
Logging in C on linux
Hi,
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!
|
|
|
10-06-2009, 06:35 AM
|
#2
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
The wrapper is in python. No wrapper in C; the basic function is a C-callable function, and the python wrapper converts the python call to a C call.
man syslog
man 3 syslog
|
|
|
10-06-2009, 11:39 AM
|
#3
|
Member
Registered: May 2009
Distribution: ubuntu
Posts: 35
Rep:
|
if you don't want to do the syslogd approach you can dup2 a file descripter to stderr.
|
|
|
10-07-2009, 02:45 AM
|
#4
|
LQ Newbie
Registered: Oct 2009
Posts: 6
Original Poster
Rep:
|
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:
#include <syslog.h>
#include <stdio.h>
int main(void)
{
printf("writing log\n");
openlog ("testProg", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
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?
Thanks in advance!
|
|
|
10-07-2009, 06:21 AM
|
#5
|
Member
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31
Rep:
|
Quote:
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.
Hope this make clear.
Srinivas
|
|
|
10-07-2009, 12:51 PM
|
#6
|
Member
Registered: May 2009
Distribution: ubuntu
Posts: 35
Rep:
|
did you restart the syslog daemon?
|
|
|
10-07-2009, 06:36 PM
|
#7
|
Member
Registered: Sep 2006
Posts: 89
Rep:
|
|
|
|
10-08-2009, 01:35 AM
|
#8
|
LQ Newbie
Registered: Oct 2009
Posts: 6
Original Poster
Rep:
|
Quote:
Originally Posted by wtruong
did you restart the syslog daemon?
|
yes,i did and even restarted my machine :-(
|
|
|
10-08-2009, 01:37 AM
|
#9
|
LQ Newbie
Registered: Oct 2009
Posts: 6
Original Poster
Rep:
|
Quote:
Originally Posted by sinu_nayak2001
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.
|
|
|
10-08-2009, 01:41 AM
|
#10
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
cross platform:
Code:
#define LOG(lf, msg) fprintf(lf, "user: %d: LOG (%u): %s\n", getuid(), time(0), (msg))
int main(){
FILE *log = fopen("log.log", "w");
LOG(log, "Program started");
LOG(log, "Hello world");
LOG(log, "Exiting...");
fclose( log);
return 0;
}
|
|
|
10-08-2009, 01:42 AM
|
#11
|
LQ Newbie
Registered: Oct 2009
Posts: 6
Original Poster
Rep:
|
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:
#include <Python.h>
#include <stdio.h>
int main(void)
{
printf("trying to call python for logging\n");
Py_Initialize();
PyRun_SimpleString("x = 78");
PyRun_SimpleString("print x");
PyRun_SimpleString("import logging");
PyRun_SimpleString("logger = logging.getLogger('myapp')");
PyRun_SimpleString("hdlr = logging.FileHandler('/usr/local/logging/myapp.log')");
PyRun_SimpleString("formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')");
PyRun_SimpleString("hdlr.setFormatter(formatter)");
PyRun_SimpleString("logger.addHandler(hdlr)");
PyRun_SimpleString("logger.setLevel(logging.INFO)");
PyRun_SimpleString("logger.error('and another log message with x value: %d',x)");
Py_Finalize();
return 0;
}
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 01:45 AM.
|
|
|
10-08-2009, 01:46 AM
|
#12
|
LQ Newbie
Registered: Oct 2009
Posts: 6
Original Poster
Rep:
|
Quote:
Originally Posted by gzunk
|
yes i had a quick glance. But didnt seem to be as rich as the python's logging.
|
|
|
All times are GMT -5. The time now is 02:30 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.
|
Latest Threads
LQ News
|
|