Commit c4b1cd63 authored by chenhaowen's avatar chenhaowen

add: logger

parent 09522e49
This diff is collapsed.
#include "config.h"
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -25,19 +26,16 @@ static void strstrip(char *s, char *chars);
void set_remote_ip(char* ip, int len)
{
memcpy(drcom_config.remote_ip, ip, len);
printf("set remote_ip = %s\n", drcom_config.remote_ip);
}
void set_keep_alive1_flag(char* flag, int len)
{
sscanf(flag, "%02hhx", &drcom_config.keep_alive1_flag);
printf("set keep_alive1_flag = %02hhx\n", drcom_config.keep_alive1_flag);
}
void set_enable_crypt(int enable)
{
drcom_config.enable_crypt = enable;
printf("set enable_crypt = %d\n", enable);
}
void get_version(char* version)
......@@ -47,10 +45,15 @@ void get_version(char* version)
void set_log_file(char * log_file, int len)
{
FILE * logp = fopen(log_file, "a");
if (!logger)
{
logger = Logger_create();
}
FILE * logp = fopen(log_file, "w");
if (logp)
{
drcom_config.log_file = logp;
logger->fp = logp;
}
}
......@@ -59,10 +62,6 @@ int parse_config(char *conf_file_name)
// char conf_file_name[256] = "/etc/gdut-drcom.conf";
char line_buf[1024];
#ifdef DEBUG
fprintf(stdout, "conf_file_name = %s\n", conf_file_name);
fflush(stdout);
#endif
FILE *fp = fopen(conf_file_name, "r");
if (fp == NULL)
......@@ -75,7 +74,10 @@ int parse_config(char *conf_file_name)
while(!feof(fp))
{
memset(line_buf, 0, sizeof(line_buf));
fgets(line_buf, sizeof(line_buf)-1, fp);
if (fgets(line_buf, sizeof(line_buf)-1, fp) == NULL)
{
continue;
}
if (strlen(line_buf) > 0)
{
parse_line(line_buf, sizeof(line_buf));
......
......@@ -4,6 +4,7 @@
#include <getopt.h>
#include "auth.h"
#include "config.h"
#include "logger.h"
static void print_help(char * name);
......@@ -12,6 +13,8 @@ int main(int argc, char *argv[])
char conf_file_name[256] = {0};
drcom_config.log_file = stdout;
logger = Logger_create();
logger->level = LOG_DEBUG;
if (argc == 1)
{
......@@ -80,10 +83,6 @@ int main(int argc, char *argv[])
// parse_config(conf_file_name);
// }
#ifdef DEBUG
fprintf(drcom_config.log_file, "drcom_config.remote_ip = %s\n", drcom_config.remote_ip);
fprintf(drcom_config.log_file, "drcom_config.remote_port = %d\n", drcom_config.remote_port);
fprintf(drcom_config.log_file, "drcom_config.keep_alive1_flag = %02hhx\n", drcom_config.keep_alive1_flag);
fprintf(drcom_config.log_file, "drcom_config.enable_crypt = %d\n", drcom_config.enable_crypt);
fflush(drcom_config.log_file);
#endif
......
#include "logger.h"
#define LOGFILE_MAXSIZE (1024*1024)
Logger * logger;
Logger * Logger_create( void )
{
Logger *l = (Logger *)malloc(sizeof(Logger));
......@@ -24,17 +28,26 @@ void Logger_free(Logger *l)
void log_add(Logger *l, int level, const char *msg)
{
static fpos_t pos;
if (level < l->level) return;
time_t meow = time(NULL);
char buf[64];
strftime(buf, sizeof(buf), l->datetime_format, localtime(&meow));
fgetpos(l->fp, &pos);
if (pos.__pos > LOGFILE_MAXSIZE)
{
rewind(l->fp);
}
fprintf(l->fp, "[%d] %c, %s : %s\n",
(int)getpid(),
LOG_LEVEL_CHARS[level],
buf,
msg);
fflush(l->fp);
}
void log_debug(Logger *l, const char *fmt, ...)
......
......@@ -35,6 +35,8 @@ void log_info(Logger *l, const char *fmt, ...);
void log_warn(Logger *l, const char *fmt, ...);
void log_error(Logger *l, const char *fmt, ...);
extern Logger * logger;
#ifdef __cplusplus
}
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment