Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging library for C [closed]

Tags:

c

logging

I am looking for a productive and simple logging library for C, which can output the log to a file. Displaying messages in the log I want to make like this:

date-time tag message

It would be nice to control the level of detail of messages and control the size of the file.

I found two projects that are suitable for me. It log4c and nglogc.

log4c seemed too big. nglogc quite fit, but also has a redundant functional. maybe you tell me more variants?

like image 857
trafalgarx Avatar asked Jun 28 '11 14:06

trafalgarx


People also ask

What is the logging library?

A logging library (or logging framework) is code that you embed into your application to create and manage log events. Logging libraries provide APIs for creating, structuring, formatting, and transmitting log events in a consistent way. Like agents, they're used to send events from your application to a destination.

What is the use of logger in C#?

Logger is used for creating customized error log files or an error can be registered as a log entry in the Windows Event Log on the administrator's machine. This article is demonstrating how to create a text based error log file along with error messages with your own format, using a C# class.


2 Answers

You can use this

File logger.h

#ifndef LOGGER_H
#define LOGGER_H

void logger(const char* tag, const char* message);

#endif /* LOG_H */

File logger.c

#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void logger(const char* tag, const char* message) {
   time_t now;
   time(&now);
   printf("%s [%s]: %s\n", ctime(&now), tag, message);
}

It's probably not perfect, but it does satisfy the needs as you have presented them.

like image 105
Edwin Buck Avatar answered Sep 21 '22 00:09

Edwin Buck


I suggest the log library which is written by myself --- zlog!

The way to fit your need in zlog is:

$ vi /etc/zlog.conf
[formats]
simple = "%D %c %m%n"
# don't know what the tag mean in your question, so put category of zlog instead
# log level is also available here, add %V means level

[rules]
my_cat.*   "xxx.log"; simple

$ vi hello.c
#include <stdio.h> 
#include "zlog.h"

int main(int argc, char** argv)
{
int rc;
zlog_category_t *c;

rc = dzlog_init("/etc/zlog.conf", "my_cat");
if (rc) {
    printf("init failed\n");
    return -1;
}

zlog_info(c, "hello, zlog");

zlog_fini();

return 0;
} 

It will generate xxx.log in current directory as

2012-09-30 07:22:50 my_cat hello, zlog

Links:

Download: https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz

UsersGuide: http://hardysimpson.github.com/zlog/UsersGuide-EN.html

Hompage: http://hardysimpson.github.com/zlog/

like image 42
HardySimpson Avatar answered Sep 22 '22 00:09

HardySimpson