From: franklahm Date: Fri, 27 Feb 2009 09:14:40 +0000 (+0000) Subject: Reworked logger X-Git-Tag: after_new_logger X-Git-Url: https://arthur.barton.de/gitweb/?a=commitdiff_plain;h=9c80488f37b84eb30db4f12303eda66468c18c8e;p=netatalk.git Reworked logger --- diff --git a/configure.in b/configure.in index 5fc23043..aa843839 100644 --- a/configure.in +++ b/configure.in @@ -1,4 +1,4 @@ -dnl $Id: configure.in,v 1.211 2009-02-16 13:49:19 franklahm Exp $ +dnl $Id: configure.in,v 1.212 2009-02-27 09:14:40 franklahm Exp $ dnl configure.in for netatalk AC_INIT(etc/afpd/main.c) @@ -206,11 +206,10 @@ NETATALK_AFS_CHECK NETATALK_CONFIG_DIRS -AC_MSG_CHECKING([whether to use logfile]) +AC_MSG_CHECKING([whether to force logfile]) AC_ARG_WITH(logfile, - [ --with-logfile=PATH path for file used by logger],[ + [ --with-logfile=PATH force logging to file PATH],[ if test x"$withval" = x"no"; then - AC_DEFINE(DISABLE_LOGGER, 1, [Define if the logger should not be used]) AC_MSG_RESULT([no, syslog]) elif test "$withval" != "yes"; then AC_DEFINE_UNQUOTED(LOGFILEPATH, "$withval", [Path to the log file]) @@ -219,12 +218,10 @@ AC_ARG_WITH(logfile, AC_DEFINE_UNQUOTED(LOGFILEPATH, "/var/log/netatalk.log", [Path to the log file]) AC_MSG_RESULT([/var/log/netatalk.log]) fi],[ - AC_DEFINE(DISABLE_LOGGER, 1, [Define if the logger should not be used]) - AC_MSG_RESULT([no, syslog]) + AC_MSG_RESULT([no, syslog]) ] ) - netatalk_cv_with_cracklib=no AC_ARG_WITH(cracklib, [ --with-cracklib=DICT enable/set location of cracklib dictionary],[ @@ -1165,6 +1162,7 @@ AC_OUTPUT([Makefile libatalk/nbp/Makefile libatalk/netddp/Makefile libatalk/util/Makefile + libatalk/util/test/Makefile libatalk/tdb/Makefile libatalk/unicode/Makefile libatalk/unicode/charsets/Makefile diff --git a/etc/afpd/afp_dsi.c b/etc/afpd/afp_dsi.c index a38bc990..673afe77 100644 --- a/etc/afpd/afp_dsi.c +++ b/etc/afpd/afp_dsi.c @@ -1,5 +1,5 @@ /* - * $Id: afp_dsi.c,v 1.33 2008-12-03 18:35:44 didg Exp $ + * $Id: afp_dsi.c,v 1.34 2009-02-27 09:14:40 franklahm Exp $ * * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu) * Copyright (c) 1990,1993 Regents of The University of Michigan. @@ -64,9 +64,6 @@ static void afp_dsi_close(AFPObj *obj) if (obj->logout) (*obj->logout)(); - /* UAM had syslog control; afpd needs to reassert itself */ - set_processname("afpd"); - syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon); LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written", dsi->read_count/1024.0, dsi->write_count/1024.0); diff --git a/etc/afpd/afp_options.c b/etc/afpd/afp_options.c index d119e6b1..e0294e77 100644 --- a/etc/afpd/afp_options.c +++ b/etc/afpd/afp_options.c @@ -1,5 +1,5 @@ /* - * $Id: afp_options.c,v 1.37 2008-05-16 04:19:41 didg Exp $ + * $Id: afp_options.c,v 1.38 2009-02-27 09:14:40 franklahm Exp $ * * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu) * Copyright (c) 1990,1993 Regents of The University of Michigan. @@ -308,121 +308,50 @@ int afp_options_parseline(char *buf, struct afp_options *options) if ((c = getoption(buf, "-server_quantum"))) options->server_quantum = strtoul(c, NULL, 0); -#ifndef DISABLE_LOGGER - /* -setuplogtype */ /* -[no]setuplog []*/ - if ((c = getoption(buf, "-setuplog"))) - { - char *ptr, *logsource, *logtype, *loglevel, *filename; - - LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c); + if ((c = getoption(buf, "-setuplog"))) { + char *ptr, *logtype, *loglevel, *filename; ptr = c; - - /* - logsource = ptr = c; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - } - } - */ + /* logtype */ logtype = ptr; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - } - } - loglevel = ptr; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { + /* get loglevel */ + ptr = strpbrk(ptr, " \t"); + if (ptr) { *ptr++ = 0; while (*ptr && isspace(*ptr)) - ptr++; - } - } - - filename = ptr; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - } + ptr++; + loglevel = ptr; + + /* get filename */ + ptr = strpbrk(ptr, " \t"); + if (ptr) { + *ptr++ = 0; + while (*ptr && isspace(*ptr)) + ptr++; + } + filename = ptr; } - LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", - logtype, loglevel, filename); - + /* finally call setuplog, filename can be NULL */ setuplog(logtype, loglevel, filename); } - if ((c = getoption(buf, "-unsetuplog"))) - { + if ((c = getoption(buf, "-unsetuplog"))) { char *ptr, *logtype, *loglevel, *filename; - LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c); - - ptr = c; - logtype = ptr; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - } - } - - loglevel = ptr; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - } - } + /* logtype */ + logtype = c; - filename = ptr; - if (ptr) - { - ptr = strpbrk(ptr, " \t"); - if (ptr) - { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - } - } - - LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", - logtype, NULL, filename); + /* get filename, can be NULL */ + strtok(c, " \t"); + filename = strtok(NULL, " \t"); + /* finally call setuplog, filename can be NULL */ setuplog(logtype, NULL, filename); } -#endif /* DISABLE_LOGGER */ + #ifdef ADMIN_GRP if ((c = getoption(buf, "-admingroup"))) { struct group *gr = getgrnam(c); @@ -794,9 +723,8 @@ int afp_options_parse(int ac, char **av, struct afp_options *options) #ifdef ultrix openlog( p, LOG_PID ); /* ultrix only */ -#else /* ultrix */ +#else set_processname(p); - syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon); #endif /* ultrix */ return 1; diff --git a/etc/afpd/auth.c b/etc/afpd/auth.c index 5bce7013..836cd375 100644 --- a/etc/afpd/auth.c +++ b/etc/afpd/auth.c @@ -1,5 +1,5 @@ /* - * $Id: auth.c,v 1.60 2009-02-26 14:05:05 franklahm Exp $ + * $Id: auth.c,v 1.61 2009-02-27 09:14:40 franklahm Exp $ * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -248,9 +248,9 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expi int admin = 0; #endif /* ADMIN_GRP */ - /* UAM had syslog control; afpd needs to reassert itself */ +#if 0 set_processname("afpd"); - syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon); +#endif if ( pwd->pw_uid == 0 ) { /* don't allow root login */ LOG(log_error, logtype_afpd, "login: root login denied!" ); diff --git a/etc/afpd/main.c b/etc/afpd/main.c index 5a944033..f7ec2f68 100644 --- a/etc/afpd/main.c +++ b/etc/afpd/main.c @@ -1,5 +1,5 @@ /* - * $Id: main.c,v 1.23 2006-08-01 09:01:32 didg Exp $ + * $Id: main.c,v 1.24 2009-02-27 09:14:40 franklahm Exp $ * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -190,8 +190,10 @@ char **av; exit(0); } +#if 0 /* Register CNID */ cnid_init(); +#endif /* install child handler for asp and dsi. we do this before afp_goaway * as afp_goaway references stuff from here. @@ -284,6 +286,9 @@ char **av; } sigprocmask(SIG_UNBLOCK, &sigs, NULL); + /* Register CNID */ + cnid_init(); + /* watch atp, dsi sockets and ipc parent/child file descriptor. */ if ((ipc = server_ipc_create())) { Ipc_fd = server_ipc_parent(ipc); diff --git a/include/atalk/logger.h b/include/atalk/logger.h index fa0a337e..9272aa7d 100644 --- a/include/atalk/logger.h +++ b/include/atalk/logger.h @@ -1,7 +1,9 @@ - #ifndef _ATALK_LOGGER_H #define _ATALK_LOGGER_H 1 +#include +#include + #include #ifdef HAVE_CONFIG_H @@ -11,19 +13,21 @@ #define MAXLOGSIZE 512 enum loglevels { - log_severe = 0, - log_error = 10, - log_warning = 20, - log_note = 30, - log_info = 40, - log_debug = 50, - log_debug6 = 60, - log_debug7 = 70, - log_debug8 = 80, - log_debug9 = 90, - log_maxdebug = 100 + log_none = 0, + log_severe = 10, + log_error = 20, + log_warning = 30, + log_note = 40, + log_info = 50, + log_debug = 60, + log_debug6 = 70, + log_debug7 = 80, + log_debug8 = 90, + log_debug9 = 100, + log_maxdebug = 110 }; #define LOGLEVEL_STRING_IDENTIFIERS { \ + "LOG_NOTHING", \ "LOG_SEVERE", \ "LOG_ERROR", \ "LOG_WARN", \ @@ -66,6 +70,9 @@ enum logtypes { /* Display Option flags. */ /* redefine these so they can don't interfeer with syslog */ /* these can be used in standard logging too */ +#define logoption_nsrcinfo 0x04 /* don't log source info */ +/* the following do not work anymore, they're only provided in order to not + * break existing source code */ #define logoption_pid 0x01 /* log the pid with each message */ #define logoption_cons 0x02 /* log on the console if error logging */ #define logoption_ndelay 0x08 /* don't delay open */ @@ -84,15 +91,59 @@ enum logtypes { #define logfacility_authpriv (10<<3) /* security/auth messages (private) */ #define logfacility_ftp (11<<3) /* ftp daemon */ -/* Setup the log filename and the loglevel, and the type of log it is. */ -/* setup the internal variables used by the logger (called automatically) */ -void log_init(); +/* ========================================================================= + Structure definitions + ========================================================================= */ + +/* Main log config */ +typedef struct { + int inited; /* file log config initialized ? */ + int filelogging; /* Any level set to filelogging ? */ + /* Deactivates syslog logging */ + char processname[16]; + int syslog_opened; /* syslog opened ? */ + int facility; /* syslog facility to use */ + int syslog_display_options; + int syslog_level; /* Log Level to send to syslog */ +} log_config_t; + +/* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */ +typedef struct { + int set; /* set individually ? yes: changing default + * doesnt change it. no: it changes it.*/ + char *filename; /* Name of file */ + int fd; /* logfiles fd */ + int level; /* Log Level to put in this file */ + int display_options; +} filelog_conf_t; + +/* ========================================================================= + Global variables + ========================================================================= */ + +#ifndef LOGGER_C +/* Make config accessible for LOG macro */ +extern log_config_t log_config; +extern filelog_conf_t file_configs[logtype_end_of_list_marker]; + +/* These are used by the LOG macro to store __FILE__ and __LINE__ */ +extern char *log_src_filename; +extern int log_src_linenumber; +#endif + +/* ========================================================================= + Global function decarations + ========================================================================= */ + +/* */ +void log_init(void); -bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype, - int display_options); +/* Setup the level and type of log that will be logged for file loggging */ +bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype); -/* Setup the Level and type of log that will be logged to syslog. */ -void syslog_setup(enum loglevels loglevel, enum logtypes logtype, +/* Setup the level and type of log that will be logged to syslog. */ +//void syslog_setup(enum loglevels loglevel); +void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility); /* void setuplog(char *logsource, char *logtype, char *loglevel, char *filename); */ @@ -104,21 +155,38 @@ void log_close(); /* This function sets up the ProcessName */ void set_processname(char *processname); -/* Log a Message */ -void make_log_entry(enum loglevels loglevel, enum logtypes logtype, - char *message, ...); - -#ifndef DISABLE_LOGGER -typedef void(*make_log_func) - (enum loglevels loglevel, enum logtypes logtype, char *message, ...); -make_log_func set_log_location(char *srcfilename, int srclinenumber); - -void LoadProccessNameFromProc(); - -#define LOG set_log_location(__FILE__, __LINE__) -#else /* DISABLE_LOGGER */ -/* if the logger is disabled the rest is a bit futile */ -#define LOG make_log_entry -#endif /* DISABLE_LOGGER */ - -#endif +/* + * How to write a LOG macro: + * http://c-faq.com/cpp/debugmacs.html + * + * We choose the verbose form in favor of the obfuscated ones, its easier + * to parse for human beings and facilitates expanding the macro for + * inline checks for debug levels. + * + * How to properly enclose multistatement macros: + * http://en.wikipedia.org/wiki/C_macro#Multiple_statements + */ + +/* LOG macro func no.1: log the message to file */ +void make_log_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...); + +/* LOG macro func no.2: log the message to syslog */ +void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...); + +/* + Note: + any configured file-logging deactivates syslog logging + */ + +#define LOG(a,b, ...) \ + do { \ + if ( ! log_config.inited) \ + log_init(); \ + if (file_configs[b].level >= a) \ + log_src_filename = __FILE__, \ + log_src_linenumber = __LINE__, \ + make_log_entry(a, b, __VA_ARGS__); \ + else if (( ! log_config.filelogging) && (log_config.syslog_level >= a)) \ + make_syslog_entry(a, b, __VA_ARGS__); \ + } while(0) +#endif /* _ATALK_LOGGER_H */ diff --git a/libatalk/util/Makefile.am b/libatalk/util/Makefile.am index e9feba13..f64d133d 100644 --- a/libatalk/util/Makefile.am +++ b/libatalk/util/Makefile.am @@ -1,5 +1,7 @@ # Makefile.am for libatalk/util/ +SUBDIRS = . test + noinst_LTLIBRARIES = libutil.la AM_CFLAGS = -I$(top_srcdir)/sys @CFLAGS@ diff --git a/libatalk/util/logger.c b/libatalk/util/logger.c index 8f1a764f..e3fd68d8 100644 --- a/libatalk/util/logger.c +++ b/libatalk/util/logger.c @@ -10,28 +10,13 @@ logger.c was written by Simon Bazley (sibaz@sibaz.com) I believe libatalk is released under the L/GPL licence. - Just incase, it is, thats the licence I'm applying to this file. - Netatalk 2001 (c) ========================================================================== Logger.c is intended as an alternative to syslog for logging - --------------------------------------------------------------- - - The initial plan is to create a structure for general information needed - to log to a file. - - Initally I'll hard code the neccesary stuff to start a log, this should - probably be moved elsewhere when some code is written to read the log - file locations from the config files. - - As a more longterm idea, I'll code this so that the data struct can be - duplicated to allow multiple concurrent log files, although this is - probably a recipe for wasted resources. - ========================================================================= */ #include @@ -42,1066 +27,418 @@ #include #include #include +#include #include #include #include + +#define LOGGER_C #include +#undef LOGGER_C -#define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0])) -#define NUMOF COUNT_ARRAY -#define KEEP_LOGFILES_OPEN -#define DO_SYSLOG -#define DO_FILELOG - -#undef DEBUG_OUTPUT_TO_SCREEN -#undef CHECK_STAT_ON_NEW_FILES -#undef CHECK_ACCESS_ON_NEW_FILES #define OPEN_LOGS_AS_UID 0 -/* ========================================================================= - External function declarations - ========================================================================= */ - -/* setup the internal variables used by the logger (called automatically) */ -void log_init(); - -/* Setup the log filename and the loglevel, and the type of log it is. */ -bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype, - int display_options); - -/* Setup the Level and type of log that will be logged to syslog. */ -void syslog_setup(enum loglevels loglevel, enum logtypes logtype, - int display_options, int facility); - -/* finish up and close the logs */ -void log_close(); - -/* This function sets up the processname */ -void set_processname(char *processname); - -/* Log a Message */ -void make_log(enum loglevels loglevel, enum logtypes logtype, - char *message, ...); -int get_syslog_equivalent(enum loglevels loglevel); - -#ifndef DISABLE_LOGGER -make_log_func set_log_location(char *srcfilename, int srclinenumber); - -/* ========================================================================= - Structure definitions - ========================================================================= */ - -/* A structure containing object level stuff */ -struct tag_log_file_data { - char log_filename[PATH_MAX]; /* Name of file */ - FILE *log_file; /* FILE pointer to file */ - enum loglevels log_level; /* Log Level to put in this file */ - int display_options; -}; - -typedef struct tag_log_file_data log_file_data_pair[2]; - -/* A structure containg class level stuff */ -struct tag_global_log_data { - int struct_size; - - char *temp_src_filename; - int temp_src_linenumber; - char processname[16]; - - int facility; - char *log_file_directory; /* Path of directory containing log files */ - log_file_data_pair **logs; -}; - -struct what_to_print_array { - bool print_datetime; - bool print_processname; - bool print_pid; - bool print_srcfile; - bool print_srcline; - bool print_errlevel; - bool print_errtype; -}; - -/* ========================================================================= - Internal function declarations - ========================================================================= */ - -void generate_message_details(char *message_details_buffer, - int message_details_buffer_length, - struct tag_log_file_data *log_struct, - enum loglevels loglevel, enum logtypes logtype); - -static char *get_command_name(char *commandpath); +#define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0])) /* ========================================================================= - Instanciated data + Config ========================================================================= */ -/* A populated instance */ - -static log_file_data_pair default_log_file_data_pair = { -{ - /*log_filename:*/ "\0\0\0\0\0\0\0\0", - /*log_file:*/ NULL, - /*log_level:*/ log_debug, - /*display_options:*/ logoption_pid -}, -{ - /*log_filename:*/ LOGFILEPATH, - /*log_file:*/ NULL, - /*log_level:*/ log_debug, - /*display_options:*/ logoption_pid -}}; +/* Main log config container, must be globally visible */ +log_config_t log_config = { + 0, /* Initialized ? 0 = no */ + 0, /* No filelogging setup yet */ + {0}, /* processname */ + 0, /* syslog opened ? */ + logfacility_daemon, /* syslog facility to use */ + logoption_ndelay|logoption_pid, /* logging options for syslog */ + 0 /* log level for syslog */ +}; -static log_file_data_pair logger_log_file_data_pair = { -{ - /*log_filename:*/ "\0\0\0\0\0\0\0\0", - /*log_file:*/ NULL, - /*log_level:*/ log_warning, - /*display_options:*/ logoption_pid -}, -{ - /*log_filename:*/ LOGFILEPATH, - /*log_file:*/ NULL, - /*log_level:*/ log_maxdebug, - /*display_options:*/ logoption_pid -}}; - -static log_file_data_pair *log_file_data_array[logtype_end_of_list_marker] = -{&default_log_file_data_pair}; - -/* The class (populated) */ -static struct tag_global_log_data global_log_data = { - /*struct_size:*/ sizeof(struct tag_global_log_data), - /*temp_src_filename:*/ NULL, - /*temp_src_linenumber:*/ 0, - /*processname:*/ "", - /*facility:*/ logfacility_daemon, - /*log_file_directory:*/ "", - /*logs:*/ NULL, +/* Default log config: log nothing to files. + 0: not set individually + NULL: Name of file + -1: logfiles fd + 0: Log Level + 0: Display options */ +#define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0} + +filelog_conf_t file_configs[logtype_end_of_list_marker] = { + DEFAULT_LOG_CONFIG, /* logtype_default */ + DEFAULT_LOG_CONFIG, /* logtype_core */ + DEFAULT_LOG_CONFIG, /* logtype_logger */ + DEFAULT_LOG_CONFIG, /* logtype_cnid */ + DEFAULT_LOG_CONFIG, /* logtype_afpd */ + DEFAULT_LOG_CONFIG, /* logtype_atalkd */ + DEFAULT_LOG_CONFIG, /* logtype_papd */ + DEFAULT_LOG_CONFIG /* logtype_uams */ }; -/* macro to get access to the array */ -#define log_file_arr (global_log_data.logs) +/* These are used by the LOG macro to store __FILE__ and __LINE__ */ +char *log_src_filename; +int log_src_linenumber; /* Array to store text to list given a log type */ -static const char * arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS; +static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS; static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings); /* Array for charachters representing log severity in the log file */ -static const char arr_loglevel_chars[] = {'S', 'E', 'W', 'N', 'I', 'D'}; +static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'}; static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars); -static const char * arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS; +static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS; static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings); -#else /* #ifndef DISABLE_LOGGER */ - char *disabled_logger_processname=NULL; -#endif /* DISABLE_LOGGER */ /* ========================================================================= - Global function definitions + Internal function definitions ========================================================================= */ -#ifndef DISABLE_LOGGER - -/* remember I'm keeping a copy of the actual char * Filename, so you mustn't - delete it, until you've finished with the log. Also you're responsible - for deleting it when you have finished with it. */ -void log_init() +void generate_message_details(char *message_details_buffer, + int message_details_buffer_length, + int display_options, + enum loglevels loglevel, enum logtypes logtype) { - if (global_log_data.logs==NULL) - { - /* first check default_log_file_data_pair */ + char *ptr = message_details_buffer; + int templen; + int len = message_details_buffer_length; - /* next clear out the log_file_data_array */ - memset(log_file_data_array, 0, sizeof(log_file_data_array)); - /* now set default_log_file_data_pairs */ - log_file_data_array[logtype_default] = &default_log_file_data_pair; - log_file_data_array[logtype_logger] = &logger_log_file_data_pair; + *ptr = 0; - /* now setup the global_log_data struct */ - global_log_data.logs = log_file_data_array; - - /* make_log_entry(log_debug, logtype_logger, "log_init ran for the first time"); */ - } -} -#endif /* #ifndef DISABLE_LOGGER */ - -bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype, - int display_options) -{ -#ifndef DISABLE_LOGGER - -#ifdef CHECK_STAT_ON_NEW_FILES - struct stat statbuf; - int firstattempt; - int retval; - gid_t gid; - uid_t uid; - int access; -#endif - char lastchar[2]; + /* Print date */ + time_t thetime; + time(&thetime); - log_file_data_pair *logs; - - log_init(); + strftime(ptr, len, "%b %d %H:%M:%S ", localtime(&thetime)); + templen = strlen(ptr); + len -= templen; + ptr += templen; - logs = log_file_arr[logtype]; - - LOG(log_info, logtype_logger, "doing log_setup, type %d, level %d, filename \"%s\"", logtype, loglevel, filename); + /* Process name */ + strncpy(ptr, log_config.processname, len); + templen = strlen(ptr); + len -= templen; + ptr += templen; + + /* PID */ + pid_t pid = getpid(); + templen = snprintf(ptr, len, "[%d]", pid); + len -= templen; + ptr += templen; - /* LOG(log_extradebug+10, logtype_logger, "checking array for logtype is malloc'd"); */ - /* has the given logtype already been assigned memory? */ - if (logs==NULL) - { - logs = (log_file_data_pair *)malloc(sizeof(log_file_data_pair)); - if (logs==NULL) - { - LOG(log_severe, logtype_logger, "can't calloc in log_setup"); + /* Source info ? */ + if ( ! (display_options & logoption_nsrcinfo)) { + templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber); + len -= templen; + ptr += templen; } - else - { - /* - memcpy(logs, log_file_arr[logtype_default], sizeof(log_file_data_pair)); - */ - log_file_arr[logtype] = logs; - (*logs)[1].log_file = NULL; - } - } - /* I think this checks if we're logging to stdout or not. Probably unused */ - if ( ((*logs)[1].log_file == stdout) && ((*logs)[1].log_file != NULL) ) - { - fclose((*logs)[1].log_file); - (*logs)[1].log_file = NULL; - } + /* Errorlevel */ + if ((loglevel/10) >= (num_loglevel_chars-1)) + templen = snprintf(ptr, len, " (%c%d:", arr_loglevel_chars[num_loglevel_chars-1], loglevel / 10 - 1); + else + templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel/10]); + len -= templen; + ptr += templen; - /* check if we need to append the given filename to a directory */ - if (strlen(global_log_data.log_file_directory)>0) - { - lastchar[0] = global_log_data. - log_file_directory[strlen(global_log_data.log_file_directory)-1]; + /* Errortype */ + const char *logtype_string; + if (logtype0) - { - retval = stat(global_log_data.log_file_directory, &statbuf); - if (retval == -1) - { -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("can't stat dir either so I'm giving up\n"); -#endif - LOG(log_severe, logtype_logger, "can't stat directory %s either", - global_log_data.log_file_directory); - return false; - } +bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype) +{ + uid_t process_uid; + + if (loglevel == 0) { + /* Disable */ + if (file_configs[logtype].set) { + if (file_configs[logtype].filename) { + free(file_configs[logtype].filename); + file_configs[logtype].filename = NULL; + } + close(file_configs[logtype].fd); + file_configs[logtype].fd = -1; + file_configs[logtype].level = 0; + file_configs[logtype].set = 0; + + /* if disabling default also set all "default using" levels to 0 */ + if (logtype == logtype_default) { + while (logtype != logtype_end_of_list_marker) { + if ( ! (file_configs[logtype].set)) + file_configs[logtype].level = 0; + logtype++; + } + } + } + + return true; } - } - -#ifdef CHECK_ACCESS_ON_NEW_FILES - access = ((statbuf.st_uid == uid)?(statbuf.st_mode & S_IWUSR):0) + - ((statbuf.st_gid == gid)?(statbuf.st_mode & S_IWGRP):0) + - (statbuf.st_mode & S_IWOTH); - if (access==0) - { -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("failing with %d, %d, %s, %s\n", log_note, logtype_logger, - "can't access Logfile %s", (*logs)[1].log_filename); -#endif + /* Safety check */ + if (NULL == filename) + return false; + + /* Resetting existing config ? */ + if (file_configs[logtype].set && file_configs[logtype].filename) { + free(file_configs[logtype].filename); + file_configs[logtype].filename == NULL; + close(file_configs[logtype].fd); + file_configs[logtype].fd = -1; + file_configs[logtype].level = 0; + file_configs[logtype].set = 0; + } - LOG(log_note, logtype_logger, "can't access file %s", - (*logs)[1].log_filename); - return false; - } -#endif /* CHECK_ACCESS_ON_NEW_FILES */ -#endif /* CHECK_STAT_ON_NEW_FILES */ -/* -#ifdef KEEP_LOGFILES_OPEN - if ((*logs)[1].log_file!=NULL) - fclose((*logs)[1].log_file); - - (*logs)[1].log_file = fopen((*logs)[1].log_filename, "at"); - if ((*logs)[1].log_file == NULL) - { - LOG(log_severe, logtype_logger, "can't open Logfile %s", - (*logs)[1].log_filename - ); - return false; - } -#endif -*/ + /* Set new values */ + file_configs[logtype].filename = strdup(filename); + file_configs[logtype].level = loglevel; + + + /* Open log file as OPEN_LOGS_AS_UID*/ + process_uid = getuid(); + setuid(OPEN_LOGS_AS_UID); + file_configs[logtype].fd = open( file_configs[logtype].filename, + O_CREAT | O_WRONLY | O_APPEND, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + setuid(process_uid); + + /* Check for error opening/creating logfile */ + if (-1 == file_configs[logtype].fd) { + free(file_configs[logtype].filename); + file_configs[logtype].filename = NULL; + file_configs[logtype].level = -1; + file_configs[logtype].set = 0; + return false; + } + + file_configs[logtype].set = 1; + log_config.filelogging = 1; + log_config.inited = 1; + + /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */ + /* which then uses the default logtype setup if it isn't setup itself: */ + /* we just copy the loglevel from default to all logtypes that are not setup. */ + /* In "make_log_entry" we then check for the logtypes if they arent setup */ + /* and use default then. We must provide accessible values for all logtypes */ + /* in order to make it easy and fast to check the loglevels in the LOG macro! */ + + if (logtype == logtype_default) { + while (logtype != logtype_end_of_list_marker) { + if ( ! (file_configs[logtype].set)) + file_configs[logtype].level = loglevel; + logtype++; + } + logtype = logtype_default; + } - LOG(log_debug7, logtype_logger, "log_file_arr[%d] now contains: " - "{log_filename:%s, log_file:%p, log_level: %d}", logtype, - (*logs)[1].log_filename, (*logs)[1].log_file, (*logs)[1].log_level); - LOG(log_debug, logtype_logger, "log_setup[%d] done", logtype); + LOG(log_info, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s", + arr_logtype_strings[logtype], arr_loglevel_strings[loglevel/10], file_configs[logtype].filename); -#endif /* DISABLE_LOGGER */ - return true; + return true; } - -void syslog_setup(enum loglevels loglevel, enum logtypes logtype, +/* logtype is ignored, it's just one for all */ +void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility) { -#ifndef DISABLE_LOGGER - log_file_data_pair *logs; - - log_init(); + log_config.syslog_level = loglevel; + log_config.syslog_display_options = display_options; + log_config.facility = facility; - logs = log_file_arr[logtype]; + log_config.inited = 1; - LOG(log_info, logtype_logger, "doing syslog_setup, type %d, level %d", logtype, loglevel); - - if (logs==NULL) - { - logs = (log_file_data_pair *)malloc(sizeof(log_file_data_pair)); - if (logs==NULL) - { - LOG(log_severe, logtype_logger, "can't calloc in log_setup"); - } - else - { - memcpy(logs, log_file_arr[logtype_default], sizeof(log_file_data_pair)); - log_file_arr[logtype] = logs; - } - } - - (*logs)[0].log_file = NULL; - (*logs)[0].log_filename[0] = 0; - (*logs)[0].log_level = loglevel; - (*logs)[0].display_options = display_options; - global_log_data.facility = facility; - - openlog(global_log_data.processname, (*logs)[0].display_options, - global_log_data.facility); - - LOG(log_debug7, logtype_logger, "log_file_arr[%d] now contains: " - "{log_filename:%s, log_file:%p, log_level: %d}", logtype, - (*logs)[0].log_filename, (*logs)[0].log_file, (*logs)[0].log_level); - LOG(log_debug, logtype_logger, "syslog_setup[%d] done", logtype); -#else /* DISABLE_LOGGER */ -/* behave like a normal openlog call */ - openlog(disabled_logger_processname, display_options, facility); -#endif /* DISABLE_LOGGER */ + LOG(log_info, logtype_logger, "Setup syslog logging: type: %s, level: %s", + arr_logtype_strings[logtype], arr_loglevel_strings[loglevel/10]); } void log_close() { -#ifndef DISABLE_LOGGER - log_file_data_pair *logs; - int n; - - LOG(log_info, logtype_logger, "log_close called"); - - for(n=(sizeof(log_file_arr)-1);n>0;n--) - { - logs = log_file_arr[n]; -#ifdef KEEP_LOGFILES_OPEN - if ((*logs)[1].log_file!=NULL) - fclose((*logs)[1].log_file); -#endif /* KEEP_LOGFILES_OPEN */ - if (logs!=NULL) - { - LOG(log_debug, logtype_logger, "freeing log entry at %d", n); -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Freeing log_data %d, stored at %p\n", n, logs); - printf("\t(filename) %s\t(type) %s\n", (*logs)[1].log_filename, - ((n= 0x20 && (c) < 0x7f) - -static char *format_text(char *fmtbuf, const char *string) -{ - int column; - const char *stringend = string + strlen(string); - char c; - int i; - - column = 0; - while (string < stringend) { - c = *string++; - - if (isprint(c)) { - fmtbuf[column] = c; - column++; - } else { - fmtbuf[column] = '\\'; - column++; - switch (c) { - case '\\': - fmtbuf[column] = '\\'; - column++; - break; - case '\a': - fmtbuf[column] = 'a'; - column++; - break; - case '\b': - fmtbuf[column] = 'b'; - column++; - break; - case '\f': - fmtbuf[column] = 'f'; - column++; - break; - case '\n': - fmtbuf[column] = 'n'; - column++; - break; - case '\r': - fmtbuf[column] = 'r'; - column++; - break; - case '\t': - fmtbuf[column] = 't'; - column++; - break; - case '\v': - fmtbuf[column] = 'v'; - column++; - break; - default: - i = (c>>6)&03; - fmtbuf[column] = i + '0'; - column++; - i = (c>>3)&07; - fmtbuf[column] = i + '0'; - column++; - i = (c>>0)&07; - fmtbuf[column] = i + '0'; - column++; - break; - } - } - } - fmtbuf[column] = '\0'; - return fmtbuf; -} /* ------------------------------------------------------------------------- - MakeLog has 1 main flaws: + make_log_entry has 1 main flaws: The message in its entirity, must fit into the tempbuffer. So it must be shorter than MAXLOGSIZE ------------------------------------------------------------------------- */ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...) { + size_t n = 0; + int fd; va_list args; char temp_buffer[MAXLOGSIZE]; - char log_buffer[4*MAXLOGSIZE]; /* fn is not reentrant but is used in signal handler * with LOGGER it's a little late source name and line number * are already changed. */ static int inlog = 0; -#ifndef DISABLE_LOGGER char log_details_buffer[MAXLOGSIZE]; -#ifdef OPEN_LOGS_AS_UID uid_t process_uid; -#endif - log_file_data_pair *logs; -#endif - if (inlog) return; inlog = 1; - -#ifndef DISABLE_LOGGER - log_init(); - - logs = log_file_arr[logtype]; - - if (logs==NULL) - { - logs = log_file_arr[logtype_default]; - } -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Making Log\n"); -#endif - -#endif /* DISABLE_LOGGER */ /* Initialise the Messages */ va_start(args, message); - vsnprintf(temp_buffer, sizeof(temp_buffer), message, args); - - /* Finished with args for now */ va_end(args); - format_text(log_buffer, temp_buffer); - -#ifdef DISABLE_LOGGER - syslog(get_syslog_equivalent(loglevel), "%s", log_buffer); -#else /* DISABLE_LOGGER */ - -#ifdef DO_SYSLOG - /* check if sysloglevel is high enough */ - if ((*logs)[0].log_level>=loglevel) - { - int sysloglevel = get_syslog_equivalent(loglevel); - - generate_message_details(log_details_buffer, sizeof(log_details_buffer), - &(*logs)[0], loglevel, logtype); - -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("About to log %s %s\n", log_details_buffer, log_buffer); - printf("about to do syslog\n"); - printf("done onw syslog\n"); -#endif - syslog(sysloglevel, "%s: %s", log_details_buffer, log_buffer); - /* - syslog(sysloglevel, "%s:%s: %s", log_levelString, - log_typeString, LogBuffer); - */ - } -#endif - -#ifdef DO_FILELOG -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("about to do the filelog\n"); -#endif - /* check if log_level is high enough */ - if ((*logs)[1].log_level>=loglevel) { - -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Open the Log, FILE* is %p\n", (*logs)[1].log_file); -#endif - /* if log isn't open, open it */ - if ((*logs)[1].log_file==NULL) { -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Opening the Log, filename is %s\n", (*logs)[1].log_filename); -#endif -#ifdef OPEN_LOGS_AS_UID - process_uid = getuid(); - setuid(OPEN_LOGS_AS_UID); -#endif - (*logs)[1].log_file = fopen((*logs)[1].log_filename, "at"); -#ifdef OPEN_LOGS_AS_UID - setuid(process_uid); -#endif - if ((*logs)[1].log_file == NULL) - { - (*logs)[1].log_file = stdout; - LOG(log_severe, logtype_logger, "can't open Logfile %s", - (*logs)[1].log_filename - ); - inlog = 0; - return; - } - } - generate_message_details(log_details_buffer, sizeof(log_details_buffer), - &(*logs)[1], loglevel, logtype); - -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Files open, lets log\n"); - printf("FILE* is %p\n", (*logs)[1].log_file); - printf("%s: %s\n", log_details_buffer, log_buffer); -#endif - - fprintf((*logs)[1].log_file, "%s: %s\n", log_details_buffer, log_buffer); + strncat(temp_buffer, "\n", MAXLOGSIZE); + + generate_message_details(log_details_buffer, sizeof(log_details_buffer), + file_configs[loglevel].set ? + file_configs[loglevel].display_options : + file_configs[logtype_default].display_options, + loglevel, logtype); + + /* Check if requested logtype is setup */ + if (file_configs[loglevel].set) + /* Yes */ + fd = file_configs[loglevel].fd; + else + /* No: use default */ + fd = file_configs[logtype_default].fd; -#ifndef KEEP_LOGFILES_OPEN - if ((*logs)[1].log_file != stdout) - { -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Closing %s\n", (*logs)[1].log_filename); -#endif - fclose((*logs)[1].log_file); - (*logs)[1].log_file = NULL; -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Closed\n"); -#endif - } -#else // KEEP_LOGFILES_OPEN - fflush((*logs)[1].log_file); -#endif // KEEP_LOGFILES_OPEN + /* If default wasnt setup its fd is -1 */ + if (fd > 0) { + write( fd, log_details_buffer, strlen(log_details_buffer) ); + write( fd, temp_buffer, strlen(temp_buffer) ); } -#endif - global_log_data.temp_src_filename = NULL; - global_log_data.temp_src_linenumber = 0; -#endif /* DISABLE_LOGGER */ inlog = 0; } -#ifndef DISABLE_LOGGER -void load_proccessname_from_proc() +/* Called by the LOG macro for syslog messages */ +void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...) { - pid_t pid = getpid(); - char buffer[PATH_MAX]; - char procname[16]; - FILE * statfile; - char *ptr; - - sprintf(buffer, "/proc/%d/stat", pid); - statfile = fopen(buffer, "rt"); - fgets(buffer, PATH_MAX-1, statfile); - fclose(statfile); - - ptr = (char *)strrchr(buffer, ')'); - *ptr = '\0'; - memset(procname, 0, sizeof procname); - sscanf(buffer, "%d (%15c", &pid, procname); /* comm[16] in kernel */ - - set_processname(procname); -} - -/* ========================================================================= - Internal function definitions - ========================================================================= */ - -static char *get_command_name(char *commandpath) -{ - char *ptr; -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("getting command name %s\n",commandpath); -#endif - ptr = (char *)strrchr(commandpath, '/'); - if (ptr==NULL) - ptr = commandpath; - else - ptr++; - -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Concluded %s\n", ptr); -#endif - return ptr; -} - -void workout_what_to_print(struct what_to_print_array *what_to_print, - struct tag_log_file_data *log_struct) -{ - /* is this a syslog entry? */ - if (log_struct->log_filename[0]==0) - { - what_to_print->print_datetime = false; - what_to_print->print_processname = false; - what_to_print->print_pid = false; - } - else - { - what_to_print->print_datetime = true; - what_to_print->print_processname = true; - - /* pid is dealt with at the syslog level if we're syslogging */ - what_to_print->print_pid = - (((log_struct->display_options & logoption_pid) == 0)?false:true); - } - - what_to_print->print_srcfile = - (((log_struct->display_options & logoption_nfile) == 0)?true:false); - what_to_print->print_srcline = - (((log_struct->display_options & logoption_nline) == 0)?true:false); - - what_to_print->print_errlevel = true; - what_to_print->print_errtype = true; -} - -void generate_message_details(char *message_details_buffer, - int message_details_buffer_length, - struct tag_log_file_data *log_struct, - enum loglevels loglevel, enum logtypes logtype) -{ -#if 0 - char datebuffer[32]; - char processinfo[64]; - char log_buffer[MAXLOGSIZE]; - const char *logtype_string; - - char loglevel_string[12]; /* max int size is 2 billion, or 10 digits */ -#endif - - char *ptr = message_details_buffer; - int templen; - int len = message_details_buffer_length; - - - struct what_to_print_array what_to_print; - - workout_what_to_print(&what_to_print, log_struct); - -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Making MessageDetails\n"); -#endif - - *ptr = 0; - /* - datebuffer[0] = 0; - ptr = datebuffer; - */ - - if (what_to_print.print_datetime) - { - time_t thetime; - time(&thetime); - - /* some people might prefer localtime() to gmtime() */ - strftime(ptr, len, "%b %d %H:%M:%S", localtime(&thetime)); -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("date is %s\n", ptr); -#endif - - templen = strlen(ptr); - len -= templen; - if (what_to_print.print_processname || what_to_print.print_pid) - strncat(ptr, " ", len); - else - strncat(ptr, ":", len); - - templen++; - len --; - ptr += templen; - } - - /* - processinfo[0] = 0; - ptr = processinfo; - */ - - if (what_to_print.print_processname) - { - strncpy(ptr, global_log_data.processname, len); - - templen = strlen(ptr); - len -= templen; - ptr += templen; - } - - if (what_to_print.print_pid) - { - pid_t pid = getpid(); - - sprintf(ptr, "[%d]", pid); - - templen = strlen(ptr); - len -= templen; - ptr += templen; - } - - if (what_to_print.print_srcfile || what_to_print.print_srcline) - { - char sprintf_buffer[8]; - char *buff_ptr=NULL; - - sprintf_buffer[0] = '['; - if (what_to_print.print_srcfile) - { - strcpy(&sprintf_buffer[1], "%s"); - buff_ptr = &sprintf_buffer[3]; - } - if (what_to_print.print_srcfile && what_to_print.print_srcline) - { - strcpy(&sprintf_buffer[3], ":"); - buff_ptr = &sprintf_buffer[4]; - } - if (what_to_print.print_srcline) - { - strcpy(buff_ptr, "%d"); - buff_ptr = &buff_ptr[2]; - } - strcpy(buff_ptr, "]"); - - /* - ok sprintf string is ready, now is the 1st parameter src or linenumber + va_list args; + char log_buffer[MAXLOGSIZE]; + /* fn is not reentrant but is used in signal handler + * with LOGGER it's a little late source name and line number + * are already changed. */ - if (what_to_print.print_srcfile) - { - sprintf(ptr, sprintf_buffer, - global_log_data.temp_src_filename, - global_log_data.temp_src_linenumber); - } - else - { - sprintf(ptr, sprintf_buffer, global_log_data.temp_src_linenumber); - } - -#ifdef DEBUG_OUTPUT_TO_SCREEN - printf("Process info is %s\n", ptr); -#endif + static int inlog = 0; - templen = strlen(ptr); - len -= templen; - ptr += templen; + if (inlog) + return; + inlog = 1; - } - - if (what_to_print.print_processname || what_to_print.print_pid || - what_to_print.print_srcfile || what_to_print.print_srcline) - { - strncat(ptr, ": ", len); - len -= 2; - ptr += 2; - } - -/* - loglevel_string[0] = 0; - ptr = loglevel_string; -*/ - - if (what_to_print.print_errlevel) - { - if ((loglevel/10) >= (num_loglevel_chars-1)) - { - sprintf(ptr, "%c%d", arr_loglevel_chars[num_loglevel_chars-1], - loglevel/10); + if ( ! (log_config.syslog_opened) ) { + openlog(log_config.processname, log_config.syslog_display_options, + log_config.facility); + log_config.syslog_opened = 1; } - else - { - sprintf(ptr, "%c", arr_loglevel_chars[loglevel/10]); - } - - templen = strlen(ptr); - len -= templen; - ptr += templen; - } - - if (what_to_print.print_errtype) - { - const char *logtype_string; - - /* get string represnetation of the Log Type */ - if (logtype []*/ void setuplog(char *logtype, char *loglevel, char *filename) { -#ifndef DISABLE_LOGGER - /* -[un]setuplog []*/ - /* - This should be rewritten so that somehow logsource is assumed and everything - can be taken from default if needs be. - */ - /* const char* sources[] = {"syslog", "filelog"}; */ int typenum, levelnum; - log_file_data_pair *logs = log_file_arr[logtype_default]; - - /* - LOG(log_extradebug, logtype_logger, "Attempting setuplog: %s %s %s %s", - logsource, logtype, loglevel, filename); - */ - LOG(log_info, logtype_logger, "setuplog is parsing logtype:%s, loglevel:%s, filename:%s", - logtype, loglevel, filename); - - if (logtype==NULL) - { - LOG(log_note, logtype_logger, "no logsource given, default is assumed"); - typenum=0; - } - else - { - for(typenum=0;typenum=num_logtype_strings) - { - LOG(log_warning, logtype_logger, "%s is not a valid log type", logtype); - } - } - if (loglevel==NULL) - { - LOG(log_note, logtype_logger, "no loglevel given, severe is assumed"); - levelnum=0; + /* Parse logtype */ + for( typenum=0; typenum < num_logtype_strings; typenum++) { + if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0) + break; } - else - { - for(levelnum=0;levelnum=num_loglevel_strings) - { - LOG(log_warning, logtype_logger, "%s is not a valid log level", loglevel); - } + if (typenum >= num_logtype_strings) { + return; } - /* sanity check */ - if ((typenum>=num_logtype_strings) || (levelnum>=num_loglevel_strings)) - { - LOG(log_warning, logtype_logger, "sanity check failed: (%s:%d), (%s:%d)", - logtype, typenum, loglevel, levelnum); - return; + /* Parse loglevel */ + if (loglevel == NULL) { + levelnum = 0; + } else { + for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) { + if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0) + break; + } + if (levelnum >= num_loglevel_strings) { + return; + } } /* now match the order of the text string with the actual enum value (10 times) */ - levelnum*=10; + levelnum *= 10; - /* is this a syslog setup or a filelog setup */ - if (filename==NULL) /* must be syslog */ - { - LOG(log_debug6, logtype_logger, "calling syslog_setup(%d, %d, ...)", levelnum, typenum); - syslog_setup(levelnum, typenum, - (*logs)[0].display_options, - global_log_data.facility); + /* is this a syslog setup or a filelog setup ? */ + if (filename == NULL) { + /* must be syslog */ + syslog_setup(levelnum, 0, + log_config.syslog_display_options, + log_config.facility); + } else { + /* this must be a filelog */ + log_setup(filename, levelnum, typenum); } - else /* this must be a filelog */ - { - LOG(log_debug6, logtype_logger, "calling log_setup(%s, %d, %d, ...)", filename, levelnum, typenum); - log_setup(filename, levelnum, typenum, - (*logs)[0].display_options); - }; + return; -#endif /* DISABLE_LOGGER */ } - - - - - - - diff --git a/libatalk/util/test/.cvsignore b/libatalk/util/test/.cvsignore new file mode 100644 index 00000000..bec03d6d --- /dev/null +++ b/libatalk/util/test/.cvsignore @@ -0,0 +1,7 @@ +Makefile +Makefile.in +logger_test +test.log +.deps +.libs + diff --git a/libatalk/util/test/Makefile.am b/libatalk/util/test/Makefile.am new file mode 100644 index 00000000..569936be --- /dev/null +++ b/libatalk/util/test/Makefile.am @@ -0,0 +1,4 @@ +bin_PROGRAMS = logger_test + +logger_test_SOURCES = logger_test.c +logger_test_LDADD = $(top_builddir)/libatalk/util/libutil.la diff --git a/libatalk/util/test/logger_test.c b/libatalk/util/test/logger_test.c index f9734d1d..7ab062e0 100644 --- a/libatalk/util/test/logger_test.c +++ b/libatalk/util/test/logger_test.c @@ -1,6 +1,4 @@ - #include -#include #include #include @@ -9,31 +7,35 @@ int main(int argc, char *argv[]) { bool retval; - /* LoadProccessNameFromProc(); */ set_processname("logger_Test"); - log_init(); - - LOG(log_severe, logtype_logger, "Logging Test starting"); - printf("Logging Test Starting\n"); - - LOG(log_debug, logtype_default, logtype_logger, "Testing 123"); - LOG(log_error, logtype_logger, "Testing 456"); - - retval = log_setup("/var/log/newlog.log", log_error, logtype_default, logoption_pid); - retval = log_setup(NULL, log_error, logtype_logger, logoption_pid); - - LOG(log_debug, logtype_default, logtype_logger, "This shouldn't log"); - LOG(log_error, logtype_logger, "This should log"); - - syslog_setup(log_error, logtype_default, logoption_pid, logfacility_user); - LOG(log_debug, logtype_default, logtype_logger, "This shouldn't log"); - LOG(log_error, logtype_logger, "This should log"); + LOG(log_severe, logtype_logger, "Logging Test starting: this should only log to syslog"); + + /* syslog testing */ + LOG(log_severe, logtype_logger, "Disabling syslog logging."); + setuplog("Default", NULL, NULL); + LOG(log_error, logtype_default, "This shouldn't log to syslog: LOG(log_error, logtype_default)."); + LOG(log_error, logtype_logger, "This shouldn't log to syslog: LOG(log_error, logtype_logger)."); + setuplog("Default", "LOG_INFO", NULL); + LOG(log_info, logtype_logger, "Set syslog logging to 'log_info', so this should log again. LOG(log_info, logtype_logger)."); + LOG(log_error, logtype_logger, "This should log to syslog: LOG(log_error, logtype_logger)."); + LOG(log_error, logtype_default, "This should log to syslog. LOG(log_error, logtype_default)."); + LOG(log_debug, logtype_logger, "This shouldn't log to syslog. LOG(log_debug, logtype_logger)."); + LOG(log_debug, logtype_default, "This shouldn't log to syslog. LOG(log_debug, logtype_default)."); + LOG(log_severe, logtype_logger, "Disabling syslog logging."); + setuplog("Default", NULL, NULL); + + /* filelog testing */ + setuplog("Default", "LOG_INFO", "test.log"); + LOG(log_info, logtype_logger, "This should log."); + LOG(log_info, logtype_default, "This should log."); + LOG(log_error, logtype_logger, "This should log."); + LOG(log_error, logtype_default, "This should log."); + LOG(log_debug, logtype_logger, "This should not log."); + LOG(log_debug, logtype_default, "This should not log."); - printf("Logging Test finishing\n"); LOG(log_severe, logtype_logger, "Logging Test finishing"); - log_close(); return 0; }