Add log_config_init_for_console() for utilities

This commit is contained in:
matt335672 2020-10-30 12:13:31 +00:00
parent 28c35f962d
commit 8d994a547d
3 changed files with 80 additions and 36 deletions

View File

@ -150,13 +150,6 @@ internal_log_start(struct log_config *l_cfg)
return ret; return ret;
} }
/* if logfile is NULL, we return error */
if (0 == l_cfg->log_file)
{
g_writeln("log_file not properly assigned");
return ret;
}
/* if progname is NULL, we return error */ /* if progname is NULL, we return error */
if (0 == l_cfg->program_name) if (0 == l_cfg->program_name)
{ {
@ -164,14 +157,20 @@ internal_log_start(struct log_config *l_cfg)
return ret; return ret;
} }
internal_log_config_dump(l_cfg); if (l_cfg->dump_on_start)
{
internal_log_config_dump(l_cfg);
}
/* open file */ /* open file */
l_cfg->fd = internal_log_file_open(l_cfg->log_file); if (l_cfg->log_file != NULL)
if (-1 == l_cfg->fd)
{ {
return LOG_ERROR_FILE_OPEN; l_cfg->fd = internal_log_file_open(l_cfg->log_file);
if (-1 == l_cfg->fd)
{
return LOG_ERROR_FILE_OPEN;
}
} }
/* if syslog is enabled, open it */ /* if syslog is enabled, open it */
@ -304,6 +303,7 @@ internal_config_read_logging(int file,
lc->console_level = LOG_LEVEL_INFO; lc->console_level = LOG_LEVEL_INFO;
lc->enable_syslog = 0; lc->enable_syslog = 0;
lc->syslog_level = LOG_LEVEL_INFO; lc->syslog_level = LOG_LEVEL_INFO;
lc->dump_on_start = 1;
lc->enable_pid = 0; lc->enable_pid = 0;
g_snprintf(section_name, 511, "%s%s", section_prefix, SESMAN_CFG_LOGGING); g_snprintf(section_name, 511, "%s%s", section_prefix, SESMAN_CFG_LOGGING);
@ -409,21 +409,40 @@ internal_log_config_dump(struct log_config *config)
{ {
char str_level[20]; char str_level[20];
#ifdef LOG_PER_LOGGER_LEVEL #ifdef LOG_PER_LOGGER_LEVEL
struct log_logger_level* logger; struct log_logger_level *logger;
int i; int i;
#endif #endif
g_printf("logging configuration:\r\n"); g_printf("logging configuration:\r\n");
internal_log_lvl2str(config->log_level, str_level); if (config->log_file)
g_printf("\tLogFile: %s\r\n", config->log_file); {
g_printf("\tLogLevel: %s\r\n", str_level); internal_log_lvl2str(config->log_level, str_level);
g_printf("\tLogFile: %s\r\n", config->log_file);
g_printf("\tLogLevel: %s\r\n", str_level);
}
else
{
g_printf("\tLogFile: %s\r\n", "<disabled>");
}
internal_log_lvl2str(config->console_level, str_level); if (config->enable_console)
g_printf("\tEnableConsole: %s\r\n", (config->enable_console ? "true" : "false")); {
internal_log_lvl2str(config->console_level, str_level);
}
else
{
g_strcpy(str_level, "<disabled>");
}
g_printf("\tConsoleLevel: %s\r\n", str_level); g_printf("\tConsoleLevel: %s\r\n", str_level);
internal_log_lvl2str(config->syslog_level, str_level); if (config->enable_syslog)
g_printf("\tEnableSyslog: %s\r\n", (config->enable_syslog ? "true" : "false")); {
internal_log_lvl2str(config->syslog_level, str_level);
}
else
{
g_strcpy(str_level, "<disabled>");
}
g_printf("\tSyslogLevel: %s\r\n", str_level); g_printf("\tSyslogLevel: %s\r\n", str_level);
#ifdef LOG_PER_LOGGER_LEVEL #ifdef LOG_PER_LOGGER_LEVEL
@ -487,6 +506,7 @@ internal_log_config_copy(struct log_config *dest, const struct log_config *src)
dest->enable_console = src->enable_console; dest->enable_console = src->enable_console;
dest->console_level = src->console_level; dest->console_level = src->console_level;
dest->enable_pid = src->enable_pid; dest->enable_pid = src->enable_pid;
dest->dump_on_start = src->dump_on_start;
for (i = 0; i < src->per_logger_level->count; ++i) for (i = 0; i < src->per_logger_level->count; ++i)
{ {
struct log_logger_level *dst_logger = struct log_logger_level *dst_logger =
@ -551,6 +571,22 @@ internal_log_location_overrides_level(const char *function_name,
* Here below the public functions * Here below the public functions
*/ */
struct log_config *
log_config_init_for_console(enum logLevels lvl)
{
struct log_config *config = internalInitAndAllocStruct();
if (config != NULL)
{
config->program_name = "<null>";
config->enable_console = 1;
config->console_level = lvl;
config->dump_on_start = 0; /* Don't need dump for console only */
}
return config;
}
struct log_config * struct log_config *
log_config_init_from_config(const char *iniFilename, log_config_init_from_config(const char *iniFilename,
const char *applicationName, const char *applicationName,
@ -755,12 +791,12 @@ log_hexdump_with_location(const char *function_name,
dump_buffer = (char *)g_malloc(dump_length, 1); dump_buffer = (char *)g_malloc(dump_length, 1);
if (dump_buffer == NULL) if (dump_buffer == NULL)
{ {
LOG_DEVEL(LOG_LEVEL_WARNING, LOG_DEVEL(LOG_LEVEL_WARNING,
"Failed to allocate buffer for hex dump of size %d", "Failed to allocate buffer for hex dump of size %d",
dump_length); dump_length);
return LOG_ERROR_MALLOC; return LOG_ERROR_MALLOC;
} }
line = (unsigned char *)src; line = (unsigned char *)src;
offset = 0; offset = 0;
@ -981,23 +1017,23 @@ internal_log_message(const enum logLevels lvl,
if (override_destination_level || lvl <= g_staticLogConfig->log_level) if (override_destination_level || lvl <= g_staticLogConfig->log_level)
{ {
/* log to application logfile */ /* log to application logfile */
#ifdef LOG_ENABLE_THREAD
pthread_mutex_lock(&(g_staticLogConfig->log_lock));
#endif
if (g_staticLogConfig->fd >= 0) if (g_staticLogConfig->fd >= 0)
{ {
#ifdef LOG_ENABLE_THREAD
pthread_mutex_lock(&(g_staticLogConfig->log_lock));
#endif
writereply = g_file_write(g_staticLogConfig->fd, buff, g_strlen(buff)); writereply = g_file_write(g_staticLogConfig->fd, buff, g_strlen(buff));
if (writereply <= 0) if (writereply <= 0)
{ {
rv = LOG_ERROR_NULL_FILE; rv = LOG_ERROR_NULL_FILE;
} }
}
#ifdef LOG_ENABLE_THREAD #ifdef LOG_ENABLE_THREAD
pthread_mutex_unlock(&(g_staticLogConfig->log_lock)); pthread_mutex_unlock(&(g_staticLogConfig->log_lock));
#endif #endif
}
} }
return rv; return rv;

View File

@ -155,6 +155,7 @@ struct log_config
int enable_syslog; int enable_syslog;
enum logLevels syslog_level; enum logLevels syslog_level;
struct list *per_logger_level; struct list *per_logger_level;
int dump_on_start;
int enable_pid; int enable_pid;
pthread_mutex_t log_lock; pthread_mutex_t log_lock;
pthread_mutexattr_t log_lock_attr; pthread_mutexattr_t log_lock_attr;
@ -268,6 +269,16 @@ log_start(const char *iniFile, const char *applicationName);
enum logReturns enum logReturns
log_start_from_param(const struct log_config *src_log_config); log_start_from_param(const struct log_config *src_log_config);
/**
* Sets up a suitable log config for writing to the console only
* (i.e. for a utility)
*
* The config can be customised by the caller before calling
* log_start_from_param()
*/
struct log_config*
log_config_init_for_console(enum logLevels lvl);
/** /**
* Read configuration from a file and store the values in the returned * Read configuration from a file and store the values in the returned
* log_config. * log_config.

View File

@ -37,8 +37,6 @@ char cmnd[257];
char serv[257]; char serv[257];
char port[257]; char port[257];
struct log_config logging;
void cmndList(struct SCP_CONNECTION *c); void cmndList(struct SCP_CONNECTION *c);
void cmndKill(struct SCP_CONNECTION *c, struct SCP_SESSION *s); void cmndKill(struct SCP_CONNECTION *c, struct SCP_SESSION *s);
void cmndHelp(void); void cmndHelp(void);
@ -56,6 +54,7 @@ int main(int argc, char **argv)
//int sel; //int sel;
int sock; int sock;
char *pwd; char *pwd;
struct log_config *logging;
user[0] = '\0'; user[0] = '\0';
pass[0] = '\0'; pass[0] = '\0';
@ -63,11 +62,9 @@ int main(int argc, char **argv)
serv[0] = '\0'; serv[0] = '\0';
port[0] = '\0'; port[0] = '\0';
logging.program_name = "sesadmin"; logging = log_config_init_for_console(LOG_LEVEL_INFO);
logging.log_file = g_strdup("xrdp-sesadmin.log"); log_start_from_param(logging);
logging.log_level = LOG_LEVEL_DEBUG; log_config_free(logging);
logging.enable_syslog = 0;
log_start_from_param(&logging);
for (idx = 0; idx < argc; idx++) for (idx = 0; idx < argc; idx++)
{ {