Skip to content
This repository was archived by the owner on May 21, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/svc_agt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 13 additions & 15 deletions include/svcagt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@
#include <stdarg.h>
#include <stdbool.h>

// 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

Expand Down
40 changes: 17 additions & 23 deletions src/svc_agt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if the logger has not been set up yet, then obviously this call won't succeed.
That's what dbg is for. See conversation

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;
}

Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -155,15 +149,15 @@ 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)
state_ = true;
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_);
Expand Down
26 changes: 12 additions & 14 deletions src/svcagt_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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);
Expand Down
24 changes: 13 additions & 11 deletions src/svcagt_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -127,15 +129,15 @@ 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)
*state = true;
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;
Expand All @@ -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;
}
Expand Down
Loading