开发者

Logging library for C [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago开发者_如何转开发.

Improve this question

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?


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.


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/


Here is mine:

log.h
------

#ifndef LOG_H 
#define LOG_H


void log_error(const char* message, ...); void log_info(const char* message, ...); void log_debug(const char* message, ...);

#endif

log.c
------
#include "log.h"


void log_format(const char* tag, const char* message, va_list args) {   time_t now;     time(&now);     char * date =ctime(&now);   date[strlen(date) - 1] = '\0';  printf("%s [%s] ", date, tag);  vprintf(message, args);     printf("\n"); }

void log_error(const char* message, ...) {  va_list args;   va_start(args, message);    log_format("error", message, args);     va_end(args); }

void log_info(const char* message, ...) {   va_list args;   va_start(args, message);    log_format("info", message, args);  va_end(args); }

void log_debug(const char* message, ...) {  va_list args;   va_start(args, message);    log_format("debug", message, args);     va_end(args); }

Have fun!


You can use this simple logging library: https://github.com/kala13x/slog

Here is an example how to use:

At first you must init log. with init_log() function. First argument is log filename, second argument is log to file (1 enabled, 0 disabled) and third argument is max log level

init_slog("example", 1, 3);

print and log something

slog(0, "Test message with level 0");
slog(2, "Test message with level 2");
slog(0, "Test message with int argument: %d", int_arg);

Outout will be something like that:

2015:04:02:56 - Test message with level 0
2015:04:02:56 - Test message with level 2
2015:04:02:56 - Test message with int argument: 69


Take a look at the zf_log logging library. It's small, simple and provides essentials only. From README.md:

This is just a thin wrapper around sprintf() function. It provides less than 20% of functionality found in more sophisticated libraries, but covers more than 80% of common use cases. Focus is made on simplicity, ease of use and performance (to be more precise - low overhead).

Features:

  • Debug logging is reduced to no-op in release builds
  • Arguments are not evaluated when the message is not logged
  • No "unused" warning for variables used in log statements only
  • Log a memory region as HEX and ASCII
  • Custom output functions


I am also finding solutions on this problem. The answer from @edwin-buck is just simple and OK for my need.

I really know nothing on multi-threading and thread-safe, but after compiling under Visual Studio compiler (it can give some warnings and tips), and searching through google, I think a few modification might make the code above thread-safe and better.

// file log.c
void log(const char* tag, const char* message) {
   time_t now;
   struct tm _calendar_time;
   char _buf[MAX_COUNT];

   time(&now);
   localtime_s(&_calendar_time, &now);

   strftime(_buf, MAX_COUNT, "%c", &_calendar_time);
   printf("%s [%s]: %s\n", _buf, tag, message);
}

Feel free to correct me if wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜