From 70ab02c76f39890b4a06aba938c14ef4e4789303 Mon Sep 17 00:00:00 2001 From: svengilat Date: Tue, 23 Aug 2016 19:28:14 +0530 Subject: [PATCH] Added new logging mechanism to integrate with rdk logger for ccspServiceManager --- include/svc_agt.h | 4 + include/svcagt_log.h | 28 ++- src/svc_agt.c | 40 ++-- src/svcagt_db.c | 26 ++- src/svcagt_files.c | 24 ++- src/svcagt_log.c | 393 ++++++------------------------------ src/svcagt_startup_states.c | 16 +- src/svcagt_systemctl.c | 7 +- src/svcagt_time.c | 6 +- tests/test_svcagt.c | 8 +- 10 files changed, 142 insertions(+), 410 deletions(-) diff --git a/include/svc_agt.h b/include/svc_agt.h index 7b8cefd..d764c78 100644 --- a/include/svc_agt.h +++ b/include/svc_agt.h @@ -20,6 +20,10 @@ // Default - not currently supported // +#define LEVEL_ERROR 0 +#define LEVEL_INFO 1 +#define LEVEL_DEBUG 2 + typedef struct { const char *svc_name; const char *goal_state; diff --git a/include/svcagt_log.h b/include/svcagt_log.h index dd44eed..e0a30e5 100644 --- a/include/svcagt_log.h +++ b/include/svcagt_log.h @@ -14,25 +14,23 @@ #include #include -// Messages are always written to log files, but when TEST_ENVIRONMENT -// is defined, messages are also displayed on the terminal screen. -//#define TEST_ENVIRONMENT 1 +/** + * @brief Handler used by svcagt_log_set_handler to receive all log + * notifications produced by the library on this function. + * + * @param level The log level + * + * @param log_msg The actual log message reported. + * + */ +typedef void (*svcagtLogHandler) (int level, const char * log_msg); -int log_init (const char *log_directory); -int log_shutdown (void); +void svcagt_log_set_handler (svcagtLogHandler handler); -void log_dbg (const char *fmt, ...); -void log_info (const char *fmt, ...); - -void log_error (const char *fmt, ...); - -void log_errno (int err, const char *fmt, ...); - -void log_not (const char *fmt, ...); - -bool log_level_is_debug (void); +void svcagt_log ( int level, const char *fmt, ...); + #endif diff --git a/src/svc_agt.c b/src/svc_agt.c index c14dbd4..68b4bd0 100644 --- a/src/svc_agt.c +++ b/src/svc_agt.c @@ -20,15 +20,12 @@ #include "svcagt_db.h" #include "svcagt_files.h" -extern void dbg (const char *fmt, ...); -extern void dbg_err (int err, const char *fmt, ...); #define RUN_STATE_RUNNING 1234 #define RUN_STATE_DONE -1234 static volatile int run_state = 0; -static char log_dirname[FILENAME_BUFLEN]; extern const char *svcagt_goal_state_str (bool state); @@ -43,48 +40,46 @@ int svc_agt_init (const char *svcagt_directory) { int err; struct stat stat_buf; - + svcagt_log (LEVEL_INFO, "processing svc_agt_init..\n"); if (NULL == svcagt_directory) { - dbg ("svc_agt init NULL svcagt directory provided\n"); + svcagt_log (LEVEL_DEBUG,"svc_agt init NULL svcagt directory provided\n"); return -1; } if (strlen(svcagt_directory) > SVCAGT_DIR_MAXLEN) { - dbg ("svc agt init svcagt directory name too long\n"); + svcagt_log (LEVEL_DEBUG,"svc agt init svcagt directory name too long\n"); return -1; } err = stat (svcagt_directory, &stat_buf); if (err != 0) { - dbg_err (errno, "directory %s cannot be accessed\n", svcagt_directory); + svcagt_log (LEVEL_ERROR, "directory %s cannot be accessed, errno:%d\n", svcagt_directory, errno); return -1; } if (!S_ISDIR (stat_buf.st_mode)) { - dbg ("%s is not a directory\n", svcagt_directory); + svcagt_log (LEVEL_DEBUG,"%s is not a directory\n", svcagt_directory); return -1; } - sprintf (log_dirname, "%s/logs", svcagt_directory); - - err = log_init (log_dirname); - if (err != 0) { - dbg ("Failed to init logger\n"); - return -1; - } if (RUN_STATE_RUNNING == run_state) { - log_error ("SVCAGT: already running at init\n"); + svcagt_log (LEVEL_ERROR, "SVCAGT: already running at init\n"); return EALREADY; } if (0 != run_state) { - log_error ("SVCAGT: not idle at init\n"); + svcagt_log (LEVEL_ERROR, "SVCAGT: not idle at init\n"); return EBUSY; } err = svcagt_files_open (svcagt_directory); if (err == 0) { + svcagt_log (LEVEL_INFO, "Initializing svcagt_db\n"); err = svcagt_db_init (); if (err == 0) + { run_state = RUN_STATE_RUNNING; - } + svcagt_log(LEVEL_DEBUG,"run_state is :%d\n", run_state); + } + svcagt_log(LEVEL_ERROR, "Error in svcagt_db_init..\n"); + } return err; } @@ -93,7 +88,6 @@ int svc_agt_shutdown (void) run_state = RUN_STATE_DONE; svcagt_db_shutdown (); svcagt_files_close (); - log_shutdown (); run_state = 0; return 0; } @@ -107,7 +101,7 @@ int svc_agt_shutdown (void) int svc_agt_get_all (service_list_item_t **service_list, bool db_query) { if (RUN_STATE_RUNNING != run_state) { - dbg ("SVCAGT: not running at get all\n"); + svcagt_log (LEVEL_DEBUG,"SVCAGT: not running at get all\n"); return -1; } return svcagt_db_get_all (service_list, db_query); @@ -132,7 +126,7 @@ int svc_agt_get (unsigned index, service_info_t *service_info, bool db_query) int err; if (RUN_STATE_RUNNING != run_state) { - dbg ("SVCAGT: not running at get\n"); + svcagt_log (LEVEL_DEBUG,"SVCAGT: not running at get\n"); return -1; } err = svcagt_db_get (index, &name, &state, db_query); @@ -155,7 +149,7 @@ int svc_agt_set (unsigned index, const char *new_state) bool state_; if (RUN_STATE_RUNNING != run_state) { - dbg ("SVCAGT: not running at set\n"); + svcagt_log (LEVEL_DEBUG,"SVCAGT: not running at set\n"); return -1; } if (strcmp (new_state, svcagt_goal_state_str (true)) == 0) @@ -163,7 +157,7 @@ int svc_agt_set (unsigned index, const char *new_state) else if (strcmp (new_state, svcagt_goal_state_str (false)) == 0) state_ = false; else { - log_error ("Invalid goal state %s\n", new_state); + svcagt_log (LEVEL_ERROR,"Invalid goal state %s\n", new_state); return -1; } return svcagt_db_set (index, state_); diff --git a/src/svcagt_db.c b/src/svcagt_db.c index c1e0d33..194aeda 100644 --- a/src/svcagt_db.c +++ b/src/svcagt_db.c @@ -67,8 +67,7 @@ int svcagt_db_init_index (void) service_count = HASH_COUNT (service_db); - log_dbg ("Creating service index\n"); - + svcagt_log (LEVEL_DEBUG, "Creating service index\n"); if (service_count == 0) { service_index = NULL; return 0; @@ -77,7 +76,7 @@ int svcagt_db_init_index (void) service_index = (struct my_service_struct **) malloc (service_count * sizeof (struct my_service_struct *)); if (service_index == NULL) { - log_error ("Unable to allocate mem for service index\n"); + svcagt_log (LEVEL_ERROR, "Unable to allocate mem for service index\n"); return -1; } @@ -105,7 +104,7 @@ int get_remaining_states_from_system () s->goal_state = (bool) state; count++; } - log_info ("Got %u states from system\n", count); + svcagt_log (LEVEL_INFO, "Got %u states from system\n", count); return 0; } @@ -163,13 +162,13 @@ int svcagt_db_add (const char *name) db_node = (struct my_service_struct*) malloc (sizeof (struct my_service_struct)); if (db_node == NULL) { - log_error ("Unable to allocate mem for new service db node\n"); + svcagt_log (LEVEL_ERROR,"Unable to allocate mem for new service db node\n"); return ENOMEM; } name_len = strlen (name); new_name = (char*) malloc (name_len+1); if (new_name == NULL) { - log_error ("Unable to allocate mem for new service name\n"); + svcagt_log (LEVEL_ERROR,"Unable to allocate mem for new service name\n"); return ENOMEM; } strcpy (new_name, name); @@ -212,7 +211,7 @@ int svcagt_db_get (unsigned index, const char **name, bool *state, bool db_query struct my_service_struct *db_node; if (index >= service_count) { - log_dbg ("Invalid index %u provided to svcagt_db_get\n", index); + svcagt_log (LEVEL_DEBUG,"Invalid index %u provided to svcagt_db_get\n", index); *name = NULL; *state = -1; return EINVAL; @@ -221,7 +220,7 @@ int svcagt_db_get (unsigned index, const char **name, bool *state, bool db_query pthread_mutex_lock (&svcagt_mutex); db_node = service_index[index]; if (!db_query) { - log_dbg ("Updating node state\n"); + svcagt_log (LEVEL_DEBUG,"Updating node state\n"); err = update_node_state (db_node); } *name = db_node->name; @@ -238,7 +237,7 @@ int svcagt_set_by_name (const char *name, bool state, long state_file_pos) HASH_FIND_STR (service_db, name, db_node); if (db_node == NULL) { - log_dbg ("set by name %s not found\n", name); + svcagt_log (LEVEL_DEBUG,"set by name %s not found\n", name); return EINVAL; } db_node->goal_state = state; @@ -273,7 +272,7 @@ int svcagt_db_set (unsigned index, bool state) struct my_service_struct *db_node; if (index >= service_count) { - log_dbg ("Invalid index %u provided to svcagt_db_set\n", index); + svcagt_log (LEVEL_DEBUG,"Invalid index %u provided to svcagt_db_set\n", index); return EINVAL; } @@ -282,14 +281,13 @@ int svcagt_db_set (unsigned index, bool state) if (db_node->state_file_pos == -1) { long file_pos; svcagt_set_service_state (db_node->name, new_value); - log_dbg ("Appending %s to goal state file\n", db_node->name); + svcagt_log (LEVEL_DEBUG,"Appending %s to goal state file\n", db_node->name); err = svcagt_goal_state_file_append (db_node->name, new_value, &file_pos); if (err == 0) db_node->state_file_pos = file_pos; } else if (new_value != db_node->goal_state) { svcagt_set_service_state (db_node->name, new_value); - log_dbg ("Updating %s:%d in goal state file at pos %ld\n", - db_node->name, new_value, db_node->state_file_pos); + svcagt_log (LEVEL_DEBUG,"Updating %s:%d in goal state file at pos %ld\n", db_node->name, new_value, db_node->state_file_pos); err = svcagt_goal_state_file_update (db_node->state_file_pos, new_value); } db_node->goal_state = new_value; @@ -310,7 +308,7 @@ int svcagt_db_get_all (service_list_item_t **service_list, bool db_query) } item_array = (service_list_item_t *) malloc (service_count * sizeof (service_list_item_t)); if (item_array == NULL) { - log_error ("Unable to allocate mem for service list\n"); + svcagt_log (LEVEL_ERROR,"Unable to allocate mem for service list\n"); return -1; } pthread_mutex_lock (&svcagt_mutex); diff --git a/src/svcagt_files.c b/src/svcagt_files.c index d20b69e..0fd2177 100644 --- a/src/svcagt_files.c +++ b/src/svcagt_files.c @@ -27,7 +27,7 @@ static char goal_states_fname[FILENAME_BUFLEN]; int seek_file_pos (long pos) { if (fseek (goal_states_fp, pos, SEEK_SET) < 0) { - log_errno (errno, "Error seeking pos %ld in file %s: ", pos, goal_states_fname); + svcagt_log (LEVEL_ERROR,"Error seeking pos %ld in file %s: ,errno:%d", pos, goal_states_fname, errno); return errno; } return 0; @@ -36,7 +36,7 @@ int seek_file_pos (long pos) int seek_file_end (void) { if (fseek (goal_states_fp, 0, SEEK_END) < 0) { - log_errno (errno, "Error seeking end of file %s: ", goal_states_fname); + svcagt_log (LEVEL_ERROR,"Error seeking end of file %s: ,errno:%d", goal_states_fname, errno); return errno; } return 0; @@ -46,7 +46,7 @@ int tell_file_pos (long *pos) { long p = ftell (goal_states_fp); if (p < 0) { - log_errno (errno, "Error getting file pos of goal states file:"); + svcagt_log (LEVEL_ERROR,"Error getting file pos of goal states file:errno:%d\n", errno); return errno; } *pos = p; @@ -63,23 +63,25 @@ int svcagt_files_open (const char *svcagt_directory) // Open the exclude file for read only exclude_fp = fopen (exclude_fname, "r"); if (NULL == exclude_fp) - log_dbg ("File %s not opened\n", exclude_fname); + + svcagt_log (LEVEL_DEBUG, "File %s not opened\n", exclude_fname); else - log_info ("Opened file %s\n", exclude_fname); + svcagt_log (LEVEL_INFO, "Opened file %s\n", exclude_fname); // Open the goal states file for read/write/append. Create if it doesn't exist. fd = open (goal_states_fname, O_CREAT | O_RDWR | O_SYNC, 0666); if (fd < 0) { - log_errno (errno, "Error(1) opening file %s: ", goal_states_fname); + svcagt_log (LEVEL_ERROR,"Error(1) opening file %s: ,errno:%d\n", goal_states_fname, errno); return errno; } goal_states_fp = fdopen (fd, "w+"); if (goal_states_fp == NULL) { - log_errno (errno, "Error(2) opening file %s: ", goal_states_fname); + svcagt_log (LEVEL_ERROR, "Error(2) opening file %s:errno:%d\n", goal_states_fname, errno); return errno; } - log_info ("Opened file %s\n", goal_states_fname); + svcagt_log (LEVEL_INFO, "Opened file %s\n", goal_states_fname); + return seek_file_pos (0); } @@ -127,7 +129,7 @@ int svcagt_goal_state_file_read (char *svc_name, bool *state, long *file_pos) return 1; } if (nfields != 2) { - log_error ("Format error in goal states file\n"); + svcagt_log (LEVEL_ERROR,"Format error in goal states file, errno:%d\n", errno); return -1; } if (state_ == (int)true) @@ -135,7 +137,7 @@ int svcagt_goal_state_file_read (char *svc_name, bool *state, long *file_pos) else if (state_ == (int)false) *state = false; else { - log_error ("Invalid state %d in goal_states file\n", state_); + svcagt_log (LEVEL_ERROR,"Invalid state %d in goal_states file, errno:%d\n", state_, errno); return -1; } return 0; @@ -150,7 +152,7 @@ int svcagt_goal_state_file_append (const char *svc_name, bool state, long *file_ err = tell_file_pos (file_pos); if (err != 0) return err; - log_dbg ("Appending %s, pos %ld to goal state file\n", svc_name, *file_pos); + svcagt_log (LEVEL_DEBUG,"Appending %s, pos %ld to goal state file\n", svc_name, *file_pos); fprintf (goal_states_fp, "%d %s\n", (int)state, svc_name); return 0; } diff --git a/src/svcagt_log.c b/src/svcagt_log.c index 3453b8f..07982f7 100644 --- a/src/svcagt_log.c +++ b/src/svcagt_log.c @@ -19,340 +19,77 @@ #include #include #include "pthread.h" +#include "svc_agt.h" #include "svcagt_time.h" -#define LEVEL_ERROR 0 -#define LEVEL_INFO 1 -#define LEVEL_DEBUG 2 - - -#ifdef TEST_ENVIRONMENT -#define MAX_FILE_SIZE 75000 -int current_level = LEVEL_DEBUG; -#else -#define MAX_FILE_SIZE 75000 -int current_level = LEVEL_INFO; -#endif - -pthread_mutex_t log_mutex=PTHREAD_MUTEX_INITIALIZER; - -#ifdef TEST_ENVIRONMENT -const char *test_log_date = NULL; // set this for testing -#endif - -const char *jwt_log_dir = NULL; - -const char *level_names[3] = {"ERROR", "INFO ", "DEBUG"}; - -int current_fd = -1; - -int current_file_num = -1; - -char current_file_date[8]; - -char current_file_name[128]; - -long current_file_size = 0; - -void print_errno (void) -{ - char errbuf[100]; - printf ("%s\n", strerror_r (errno, errbuf, 100)); -} - -void dbg (const char *fmt, ...) -{ -#ifdef TEST_ENVIRONMENT - va_list arg_ptr; - va_start(arg_ptr, fmt); - vprintf(fmt, arg_ptr); - va_end(arg_ptr); -#endif -} - -void dbg_err (int err, const char *fmt, ...) -{ -#ifdef TEST_ENVIRONMENT - char errbuf[100]; - - va_list arg_ptr; - va_start(arg_ptr, fmt); - vprintf(fmt, arg_ptr); - va_end(arg_ptr); - printf ("%s\n", strerror_r (err, errbuf, 100)); -#endif -} - -static int my_current_date (char *date) -{ -#ifdef TEST_ENVIRONMENT - if (test_log_date != NULL) { - if (strlen (test_log_date) == 8) { - strncpy (date, test_log_date, 8); - return 0; - } - test_log_date += 1; - } -#endif - return get_current_date (date); -} - - -static void make_file_name (void) -{ - sprintf (current_file_name, "%s/log%8.8s.%d", - jwt_log_dir, current_file_date, current_file_num); -} - -bool log_level_is_debug (void) -{ - return (current_level == LEVEL_DEBUG); -} - -int get_valid_file_num (const char *file_name, const char *date) -{ -#define IS_DIGIT(c) (((c)>='0') && ((c)<='9')) - char c; - int i, val; - if (strncmp (file_name, "log", 3) != 0) - return -1; - if (strncmp (file_name+3, date, 8) != 0) - return -1; - if (file_name[11] != '.') - return -1; - val = 0; - if (file_name[12] == 0) - return -1; - for (i=12; (c=file_name[i]) != 0; i++) { - if (!IS_DIGIT (c)) - return -1; - val = (val*10) + (c-'0'); - } - if (val == 0) - return -1; - return val; -} - -int get_last_file_num_in_dir (const char *date, const char *log_dir) -{ - int file_num = -1; - struct dirent *dent; - DIR *dirp = opendir (log_dir); - - if (dirp == NULL) { - dbg_err (errno, "Could not open log directory %s\n", log_dir); - return -1; - } - - while (1) { - int new_file_num = -1; - - dent = readdir (dirp); - if (dent == NULL) - break; - if (dent->d_type != DT_REG) - continue; - new_file_num = get_valid_file_num (dent->d_name, date); - if (new_file_num > file_num) - file_num = new_file_num; - } - - closedir (dirp); - return file_num; -} - -int get_last_file_num (const char *date) -{ - return get_last_file_num_in_dir (date, jwt_log_dir); -} - -static void close_current_file (void) -{ - if (current_fd != -1) { - close (current_fd); - current_fd = -1; - } -} - -static int get_current_file_size (void) -{ - int err; - struct stat file_stat; - - err = fstat (current_fd, &file_stat); - if (err != 0) { - dbg_err (errno, "Could not stat file %s\n", current_file_name); - close_current_file (); - return -1; - } - current_file_size = (long) file_stat.st_size; - return 0; -} - -static int open_next_file (void); - -static int open_file (bool init) -{ - int flags = O_WRONLY | O_CREAT | O_SYNC; - - if (init) - flags |= O_APPEND; - else - flags |= O_TRUNC; - - make_file_name (); - current_fd = open (current_file_name, flags, 0666); - if (current_fd == -1) { - dbg_err (errno, "Unable to open log file %s\n", current_file_name); - return -1; - } - current_file_size = 0; - if (init) { - if (get_current_file_size () != 0) - return -1; - if (current_file_size >= MAX_FILE_SIZE) - return open_next_file (); - } - return 0; -} - - -static int open_next_file (void) -{ - int err; - char current_date[8]; - - close_current_file (); - - err = my_current_date (current_date); - if (err != 0) - return err; - if (strncmp (current_date, current_file_date, 8) == 0) { - current_file_num++; - } else { - strncpy (current_file_date, current_date, 8); - current_file_num = 1; - } - - return open_file (false); -} - - -int log_init (const char *log_directory) -{ - int err; - - if (NULL == log_directory) { - dbg ("Null log directory given\n"); - return -1; - } - jwt_log_dir = log_directory; - err = mkdir (jwt_log_dir, 0777); - if ((err == -1) && (errno != EEXIST)) { - dbg_err (errno, "Failed to create directory %s\n", log_directory); - return -1; - } - err = my_current_date (current_file_date); - if (err != 0) - return err; - dbg ("Current date in logger is %8.8s\n", current_file_date); - current_file_num = get_last_file_num (current_file_date); - if (current_file_num <= 0) - current_file_num = 1; - return open_file (true); -} - -int log_shutdown (void) -{ - close_current_file (); - return 0; -} - -#define MSG_BUF_SIZE 512 - -int output_log (int level, const char *fmt, va_list arg_ptr, int err_code) -{ - int err = 0; - char timestamp[TIMESTAMP_BUFLEN]; - char msg_buf[MSG_BUF_SIZE]; - char errbuf[100]; - const char *err_msg; - int header_len, error_len, buf_limit, nbytes; - - err = make_current_timestamp (timestamp); - if (err != 0) - return err; - - header_len = sprintf (msg_buf, "[%s] %s: ", timestamp, level_names[level]); - if (header_len < 0) - return -1; - - buf_limit = MSG_BUF_SIZE - header_len; - error_len = 0; - if (err_code != 0) { - err_msg = strerror_r (errno, errbuf, 100); - error_len = strlen (err_msg)+2; - buf_limit -= error_len; - } - - nbytes = vsnprintf(msg_buf+header_len, buf_limit, fmt, arg_ptr); - if (nbytes < 0) - return -1; - nbytes = strlen(msg_buf); - if (err_code != 0) { - strcpy (msg_buf+nbytes, ": "); - strcpy (msg_buf+nbytes+2, err_msg); - nbytes += error_len; - } - -#ifdef TEST_ENVIRONMENT - write (STDOUT_FILENO, msg_buf, nbytes); -#endif - - pthread_mutex_lock (&log_mutex); +/* log handling */ +svcagtLogHandler log_handler; + +/** + * @brief Allows to define a log handler that will receive all logs + * produced under the provided content. + * + * @param handler The handler to be called for each log to be + * notified. Passing in NULL is allowed to remove any previously + * configured handler. + * + */ +void svcagt_log_set_handler (svcagtLogHandler handler) +{ + log_handler = handler; + return; +} + + +/** + * + * The function allows to provide a printf like interface to report + * messages. + * + * @param level The level that this message is classified. + * + * @param message The message to report. The message to report must be + * not NULL. + */ +void svcagt_log ( int level, const char *msg, ...) +{ + char *pTempChar = NULL; + int ret = 0; - if (current_fd != -1) { - write (current_fd, msg_buf, nbytes); - current_file_size += nbytes; - if (current_file_size >= MAX_FILE_SIZE) { - err = open_next_file (); + if (log_handler) + { + va_list arg_ptr; + pTempChar = (char *)malloc(4096); + + if(pTempChar) + { + va_start(arg_ptr, msg); + ret = vsnprintf(pTempChar, 4096, msg, arg_ptr); + if(ret < 0) + { + perror(pTempChar); + } + + va_end(arg_ptr); + + log_handler (level, pTempChar); + + if(pTempChar !=NULL) + { + free(pTempChar); + pTempChar = NULL; + } + + return; } + printf("Error in log msg allocation\n"); + return; + } - pthread_mutex_unlock (&log_mutex); - return err; -} - -#define BUF_PRINT(level, fmt, err_code) \ - va_list arg_ptr; \ - va_start(arg_ptr, fmt); \ - output_log (level, fmt, arg_ptr, err_code); \ - va_end(arg_ptr); - -void log_not (const char *fmt, ...) -{ -} - -void log_dbg (const char *fmt, ...) -{ - if (current_level >= LEVEL_DEBUG) { - BUF_PRINT (LEVEL_DEBUG, fmt, 0); - } -} - -void log_info (const char *fmt, ...) -{ - if (current_level >= LEVEL_INFO) { - BUF_PRINT (LEVEL_INFO, fmt, 0); - } -} + + printf("log_handler is not set\n"); + return; -void log_error (const char *fmt, ...) -{ - BUF_PRINT (LEVEL_ERROR, fmt, 0); } -void log_errno (int err, const char *fmt, ...) -{ - BUF_PRINT (LEVEL_ERROR, fmt, err); -} diff --git a/src/svcagt_startup_states.c b/src/svcagt_startup_states.c index b68e4b4..fd0293d 100644 --- a/src/svcagt_startup_states.c +++ b/src/svcagt_startup_states.c @@ -20,7 +20,7 @@ #include "svcagt_db.h" #define SERVICES_DIR1 "/etc/systemd/system" -#define SERVICES_DIR2 "/usr/lib/systemd/system" +#define SERVICES_DIR2 "/lib/systemd/system" const char *services_dir1 = SERVICES_DIR1; const char *services_dir2 = SERVICES_DIR2; @@ -58,12 +58,12 @@ int find_services_ (const char *services_dir) DIR *dirp = opendir (services_dir); char svc_name[SVCAGT_SVC_NAME_BUFLEN]; - if (dirp == NULL) { - log_errno (errno, "Could not open systemd directory %s\n", services_dir); + if (dirp == NULL) { + svcagt_log (LEVEL_ERROR,"Could not open systemd directory %s, errno:%d\n", services_dir, errno); return -1; } - log_info ("Finding services in %s\n", services_dir); + svcagt_log (LEVEL_INFO, "Finding services in %s\n", services_dir); service_count = 0; while (1) { dent = readdir (dirp); @@ -82,7 +82,7 @@ int find_services_ (const char *services_dir) } closedir (dirp); - log_info ("Found %d services in %s\n", service_count, services_dir); + svcagt_log (LEVEL_INFO, "Found %d services in %s\n", service_count, services_dir); return 0; } @@ -137,7 +137,7 @@ int remove_excluded_services (void) count++; } } - log_info ("Excluded %d services\n", count); + svcagt_log (LEVEL_INFO, "Excluded %d services\n", count); return 0; } @@ -151,7 +151,7 @@ int set_states_from_goals_file (void) long file_pos; char name_buf[SVCAGT_SVC_NAME_BUFLEN]; - log_info ("Restoring service states from goals file\n"); + svcagt_log (LEVEL_INFO, "Restoring service states from goals file\n"); while (1) { err = svcagt_goal_state_file_read (name_buf, &state, &file_pos); if (err != 0) @@ -160,7 +160,7 @@ int set_states_from_goals_file (void) if (err == 0) count++; } - log_info ("Restored %d service states\n", count); + svcagt_log (LEVEL_INFO, "Restored %d service states\n", count); if (err == 1) // EOF return 0; return err; diff --git a/src/svcagt_systemctl.c b/src/svcagt_systemctl.c index 1a7bb37..7705fa3 100644 --- a/src/svcagt_systemctl.c +++ b/src/svcagt_systemctl.c @@ -14,6 +14,7 @@ #include #include #include "svcagt_systemctl.h" +#include "svc_agt.h" #include "svcagt_log.h" @@ -30,7 +31,7 @@ int svcagt_get_service_state (const char *svc_name) sprintf (cmdbuf, "%s is-active %s.service", svcagt_systemctl_cmd, svc_name); exit_code = system (cmdbuf); if (exit_code == -1) { - log_errno (errno, "Error invoking systemctl command\n"); + svcagt_log (LEVEL_ERROR,"Error invoking systemctl command, errno:%d\n", errno); return -1; } running = (exit_code == 0); @@ -53,12 +54,12 @@ int svcagt_set_service_state (const char *svc_name, bool state) cmd_option = "stop"; } - log_info ("%s %s\n", start_stop_msg, svc_name); + svcagt_log (LEVEL_INFO,"%s %s\n", start_stop_msg, svc_name); sprintf (cmdbuf, "%s %s %s.service", svcagt_systemctl_cmd, cmd_option, svc_name); exit_code = system (cmdbuf); if (exit_code != 0) - log_errno (errno, "Command %s failed\n", cmdbuf); + svcagt_log (LEVEL_ERROR,"Command %s failed, errno:%d\n", cmdbuf, errno); return exit_code; } diff --git a/src/svcagt_time.c b/src/svcagt_time.c index 800e513..9ea0706 100644 --- a/src/svcagt_time.c +++ b/src/svcagt_time.c @@ -10,14 +10,12 @@ ******************************************************************************/ #include "svcagt_time.h" -#include "svcagt_log.h" +#include "svc_agt.h" #include #include #include -extern void dbg (const char *fmt, ...); -extern void dbg_err (int err, const char *fmt, ...); /** * struct tv has two components: tv_sec and tv_usec @@ -38,7 +36,7 @@ int get_current_time (struct timeval *tv, struct tm *split_time) time_t secs; int err = gettimeofday (tv, NULL); if (err != 0) { - dbg_err (err, "Error getting time of day: "); + svcagt_log (LEVEL_ERROR, "Error getting time of day:err:%d\n", err); return err; } secs = (time_t) tv->tv_sec; diff --git a/tests/test_svcagt.c b/tests/test_svcagt.c index 60aff77..a7dfdc7 100644 --- a/tests/test_svcagt.c +++ b/tests/test_svcagt.c @@ -25,8 +25,8 @@ extern const char *svcagt_goal_state_str (bool state); extern bool svcagt_suppress_init_states; extern void set_test_services (const char *test_services_dir1, const char *test_services_dir2); extern const char *svcagt_systemctl_cmd; -extern void dbg (const char *fmt, ...); -extern void dbg_err (int err, const char *fmt, ...); +//extern void dbg (const char *fmt, ...); +//extern void dbg_err (int err, const char *fmt, ...); static const char *running_str = "Running"; static const char *stopped_str = "Stopped"; @@ -75,7 +75,7 @@ int check_mock_systemctl_running (void) err = stat (name_buf, &stat_buf); if (err != 0) { printf ("mock_systemctl should be started\n"); - dbg_err (errno, "unable to stat %s\n", name_buf); + //dbg_err (errno, "unable to stat %s\n", name_buf); return -1; } if (!S_ISDIR (stat_buf.st_mode)) { @@ -763,7 +763,7 @@ int pass_fail_tests (void) err = remove ("./svcagt_goal_states.txt"); if ((err != 0) && (err != ENOENT)) { - dbg_err (errno, "Error removing goal states file\n"); + //dbg_err (errno, "Error removing goal states file\n"); return -1; }