LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 10-06-2009, 05:59 AM   #1
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6
Thanked: 0
Logging in C on linux


[Log in to get rid of this advertisement]
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!
linuxubuntu middlewhere is offline  
Tag This Post , ,
Reply With Quote
Old 10-06-2009, 06:35 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Distribution: mandriva 2009.1, 2008.1, kubuntu 8, fedora 8
Posts: 2,819
Thanked: 38
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
linuxmandriva jiml8 is offline     Reply With Quote
Thanked by:
Old 10-06-2009, 11:39 AM   #3
wtruong
LQ Newbie
 
Registered: May 2009
Distribution: ubuntu
Posts: 18
Thanked: 1
if you don't want to do the syslogd approach you can dup2 a file descripter to stderr.
windows_xp_2003 wtruong is offline     Reply With Quote
Thanked by:
Old 10-07-2009, 02:45 AM   #4
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6
Thanked: 0

Original Poster
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!
linuxubuntu middlewhere is offline     Reply With Quote
Old 10-07-2009, 06:21 AM   #5
sinu_nayak2001
LQ Newbie
 
Registered: Oct 2009
Posts: 11
Thanked: 0
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
windows_xp_2003 sinu_nayak2001 is offline     Reply With Quote
Old 10-07-2009, 12:51 PM   #6
wtruong
LQ Newbie
 
Registered: May 2009
Distribution: ubuntu
Posts: 18
Thanked: 1
did you restart the syslog daemon?
windows_xp_2003 wtruong is offline     Reply With Quote
Old 10-07-2009, 06:36 PM   #7
gzunk
Member
 
Registered: Sep 2006
Posts: 81
Thanked: 7
Have you looked at Log4c? http://log4c.sourceforge.net/
windows_vista gzunk is offline     Reply With Quote
Old 10-08-2009, 01:35 AM   #8
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6
Thanked: 0

Original Poster
Quote:
Originally Posted by wtruong View Post
did you restart the syslog daemon?
yes,i did and even restarted my machine :-(
linuxubuntu middlewhere is offline     Reply With Quote
Old 10-08-2009, 01:37 AM   #9
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6
Thanked: 0

Original Poster
Quote:
Originally Posted by sinu_nayak2001 View Post
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.
linuxubuntu middlewhere is offline     Reply With Quote
Old 10-08-2009, 01:41 AM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 1,239
Thanked: 60
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;
}
windows_vista smeezekitty is offline     Reply With Quote
Old 10-08-2009, 01:42 AM   #11
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6
Thanked: 0

Original Poster
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..
linuxubuntu middlewhere is offline     Reply With Quote
Old 10-08-2009, 01:46 AM   #12
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6
Thanked: 0

Original Poster
Quote:
Originally Posted by gzunk View Post
Have you looked at Log4c? http://log4c.sourceforge.net/
yes i had a quick glance. But didnt seem to be as rich as the python's logging.
linuxubuntu middlewhere is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Logging in and logging out of a server in a script frankie_DJ Linux - Newbie 4 01-28-2007 12:03 AM
HELP-Logging into Linux jcskid85 Linux - Software 3 09-14-2004 03:01 AM
using red-carpet without logging out and logging as root. packman Linux - Software 1 12-09-2002 03:55 AM
linux not logging brianm1 Linux - General 4 08-05-2002 01:20 PM
Logging on to WINS with Linux AnyoneEB Linux - Networking 2 06-11-2002 12:05 AM


All times are GMT -5. The time now is 02:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration