LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 10-06-2009, 04:59 AM   #1
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 0
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!
 
Old 10-06-2009, 05:35 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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
 
Old 10-06-2009, 10:39 AM   #3
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Rep: Reputation: 16
if you don't want to do the syslogd approach you can dup2 a file descripter to stderr.
 
Old 10-07-2009, 01:45 AM   #4
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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!
 
Old 10-07-2009, 05:21 AM   #5
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Rep: Reputation: 20
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
 
Old 10-07-2009, 11:51 AM   #6
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Rep: Reputation: 16
did you restart the syslog daemon?
 
Old 10-07-2009, 05:36 PM   #7
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
Have you looked at Log4c? http://log4c.sourceforge.net/
 
Old 10-08-2009, 12:35 AM   #8
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6

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

Original Poster
Rep: Reputation: 0
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.
 
Old 10-08-2009, 12:41 AM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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;
}
 
Old 10-08-2009, 12:42 AM   #11
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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 12:45 AM.
 
Old 10-08-2009, 12:46 AM   #12
middlewhere
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
libraries, logging, syslog



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-27-2007 11:03 PM
HELP-Logging into Linux jcskid85 Linux - Software 3 09-14-2004 02:01 AM
using red-carpet without logging out and logging as root. packman Linux - Software 1 12-09-2002 02:55 AM
linux not logging brianm1 Linux - General 4 08-05-2002 12:20 PM
Logging on to WINS with Linux AnyoneEB Linux - Networking 2 06-10-2002 11:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration