2012-09-20 07:51:34 +04:00
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
2014-03-02 11:26:40 +04:00
|
|
|
* Copyright (C) Jay Sorg 2004-2014
|
2012-09-20 07:51:34 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2005-10-24 01:50:42 +04:00
|
|
|
|
2006-08-23 07:45:56 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2012-05-27 19:17:39 +04:00
|
|
|
#include "list.h"
|
2012-06-04 23:51:22 +04:00
|
|
|
#include "file.h"
|
2005-10-24 01:50:42 +04:00
|
|
|
#include "os_calls.h"
|
2012-06-04 23:51:22 +04:00
|
|
|
#include "thread_calls.h"
|
2005-10-24 01:50:42 +04:00
|
|
|
|
2012-06-04 23:51:22 +04:00
|
|
|
/* Add a define here so that the log.h will hold more information
|
2012-05-27 19:17:39 +04:00
|
|
|
* when compiled from this C file.
|
2012-06-04 23:51:22 +04:00
|
|
|
* When compiled normally the log.h file only contain the public parts
|
2012-05-27 19:17:39 +04:00
|
|
|
* of the operators in this file. */
|
|
|
|
#define LOGINTERNALSTUFF
|
2005-10-24 01:50:42 +04:00
|
|
|
#include "log.h"
|
|
|
|
|
2012-05-27 19:17:39 +04:00
|
|
|
/* Here we store the current state and configuration of the log */
|
2014-07-26 09:42:07 +04:00
|
|
|
static struct log_config *g_staticLogConfig = NULL;
|
2012-05-27 19:17:39 +04:00
|
|
|
|
2012-06-04 23:51:22 +04:00
|
|
|
/* This file first start with all private functions.
|
|
|
|
In the end of the file the public functions is defined */
|
2012-05-27 19:17:39 +04:00
|
|
|
|
2005-10-24 01:50:42 +04:00
|
|
|
/**
|
|
|
|
*
|
2006-06-04 16:10:02 +04:00
|
|
|
* @brief Opens log file
|
2005-10-24 01:50:42 +04:00
|
|
|
* @param fname log file name
|
|
|
|
* @return see open(2) return values
|
2012-06-04 23:51:22 +04:00
|
|
|
*
|
2005-10-24 01:50:42 +04:00
|
|
|
*/
|
2012-05-27 19:17:39 +04:00
|
|
|
int DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internal_log_file_open(const char *fname)
|
2005-10-24 01:50:42 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (fname != NULL)
|
|
|
|
{
|
|
|
|
ret = open(fname, O_WRONLY | O_CREAT | O_APPEND | O_SYNC,
|
|
|
|
S_IRUSR | S_IWUSR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2005-10-24 01:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2006-06-04 16:10:02 +04:00
|
|
|
* @brief Converts xrdp log level to syslog logging level
|
2005-10-24 01:50:42 +04:00
|
|
|
* @param xrdp logging level
|
|
|
|
* @return syslog equivalent logging level
|
|
|
|
*
|
|
|
|
*/
|
2012-05-27 19:17:39 +04:00
|
|
|
int DEFAULT_CC
|
|
|
|
internal_log_xrdp2syslog(const enum logLevels lvl)
|
2005-10-24 01:50:42 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
switch (lvl)
|
|
|
|
{
|
|
|
|
case LOG_LEVEL_ALWAYS:
|
|
|
|
return LOG_CRIT;
|
|
|
|
case LOG_LEVEL_ERROR:
|
|
|
|
return LOG_ERR;
|
|
|
|
case LOG_LEVEL_WARNING:
|
|
|
|
return LOG_WARNING;
|
|
|
|
case LOG_LEVEL_INFO:
|
|
|
|
return LOG_INFO;
|
|
|
|
case LOG_LEVEL_DEBUG:
|
|
|
|
return LOG_DEBUG;
|
|
|
|
default:
|
|
|
|
g_writeln("Undefined log level - programming error");
|
|
|
|
return LOG_DEBUG;
|
|
|
|
}
|
2005-10-24 01:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-27 19:17:39 +04:00
|
|
|
* @brief Converts xrdp log levels to textual logging levels
|
2006-06-04 16:10:02 +04:00
|
|
|
* @param lvl logging level
|
2012-05-27 19:17:39 +04:00
|
|
|
* @param str pointer to a string, must be allocated before
|
|
|
|
* @return The log string in str pointer.
|
2005-10-24 01:50:42 +04:00
|
|
|
*
|
|
|
|
*/
|
2012-05-27 19:17:39 +04:00
|
|
|
void DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internal_log_lvl2str(const enum logLevels lvl, char *str)
|
2005-10-24 01:50:42 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
switch (lvl)
|
|
|
|
{
|
|
|
|
case LOG_LEVEL_ALWAYS:
|
|
|
|
snprintf(str, 9, "%s", "[CORE ] ");
|
|
|
|
break;
|
|
|
|
case LOG_LEVEL_ERROR:
|
|
|
|
snprintf(str, 9, "%s", "[ERROR] ");
|
|
|
|
break;
|
|
|
|
case LOG_LEVEL_WARNING:
|
|
|
|
snprintf(str, 9, "%s", "[WARN ] ");
|
|
|
|
break;
|
|
|
|
case LOG_LEVEL_INFO:
|
|
|
|
snprintf(str, 9, "%s", "[INFO ] ");
|
|
|
|
break;
|
|
|
|
case LOG_LEVEL_DEBUG:
|
|
|
|
snprintf(str, 9, "%s", "[DEBUG] ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snprintf(str, 9, "%s", "PRG ERR!");
|
|
|
|
g_writeln("Programming error - undefined log level!!!");
|
|
|
|
}
|
2005-10-24 01:50:42 +04:00
|
|
|
}
|
2006-08-21 07:17:39 +04:00
|
|
|
|
2005-10-24 01:50:42 +04:00
|
|
|
/******************************************************************************/
|
2012-05-27 19:17:39 +04:00
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internal_log_start(struct log_config *l_cfg)
|
2005-10-24 01:50:42 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
2005-10-24 01:50:42 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (0 == l_cfg)
|
|
|
|
{
|
|
|
|
ret = LOG_ERROR_MALLOC;
|
|
|
|
return ret;
|
|
|
|
}
|
2006-08-21 07:17:39 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
/* if logfile is NULL, we return error */
|
|
|
|
if (0 == l_cfg->log_file)
|
|
|
|
{
|
|
|
|
g_writeln("log_file not properly assigned");
|
|
|
|
return ret;
|
|
|
|
}
|
2006-08-21 07:17:39 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
/* if progname is NULL, we ureturn error */
|
|
|
|
if (0 == l_cfg->program_name)
|
|
|
|
{
|
|
|
|
g_writeln("program_name not properly assigned");
|
|
|
|
return ret;
|
|
|
|
}
|
2005-10-24 01:50:42 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
/* open file */
|
|
|
|
l_cfg->fd = internal_log_file_open(l_cfg->log_file);
|
2005-10-24 01:50:42 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (-1 == l_cfg->fd)
|
|
|
|
{
|
|
|
|
return LOG_ERROR_FILE_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if syslog is enabled, open it */
|
|
|
|
if (l_cfg->enable_syslog)
|
|
|
|
{
|
|
|
|
openlog(l_cfg->program_name, LOG_CONS | LOG_PID, LOG_DAEMON);
|
|
|
|
}
|
2005-10-24 01:50:42 +04:00
|
|
|
|
2006-06-04 16:10:02 +04:00
|
|
|
#ifdef LOG_ENABLE_THREAD
|
2012-09-20 07:51:34 +04:00
|
|
|
pthread_mutexattr_init(&(l_cfg->log_lock_attr));
|
|
|
|
pthread_mutex_init(&(l_cfg->log_lock), &(l_cfg->log_lock_attr));
|
2006-06-04 16:10:02 +04:00
|
|
|
#endif
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
return LOG_STARTUP_OK;
|
2005-10-24 01:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2012-05-27 19:17:39 +04:00
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internal_log_end(struct log_config *l_cfg)
|
2005-10-24 01:50:42 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
|
|
|
|
|
|
|
/* if log is closed, quit silently */
|
|
|
|
if (0 == l_cfg)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* closing log file */
|
|
|
|
log_message(LOG_LEVEL_ALWAYS, "shutting down log subsystem...");
|
|
|
|
|
|
|
|
if (0 > l_cfg->fd)
|
|
|
|
{
|
|
|
|
/* closing logfile... */
|
|
|
|
g_file_close(l_cfg->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if syslog is enabled, close it */
|
|
|
|
if (l_cfg->enable_syslog)
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* freeing allocated memory */
|
|
|
|
if (0 != l_cfg->log_file)
|
|
|
|
{
|
|
|
|
g_free(l_cfg->log_file);
|
|
|
|
l_cfg->log_file = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != l_cfg->program_name)
|
|
|
|
{
|
|
|
|
g_free(l_cfg->program_name);
|
|
|
|
l_cfg->program_name = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = LOG_STARTUP_OK;
|
2012-05-27 19:17:39 +04:00
|
|
|
return ret;
|
2005-10-24 01:50:42 +04:00
|
|
|
}
|
|
|
|
|
2012-05-27 19:17:39 +04:00
|
|
|
/**
|
|
|
|
* Converts a string representing th log level to a value
|
|
|
|
* @param buf
|
2012-06-04 23:51:22 +04:00
|
|
|
* @return
|
2012-05-27 19:17:39 +04:00
|
|
|
*/
|
|
|
|
enum logLevels DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internal_log_text2level(char *buf)
|
2006-08-21 07:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
if (0 == g_strcasecmp(buf, "0") ||
|
|
|
|
0 == g_strcasecmp(buf, "core"))
|
|
|
|
{
|
|
|
|
return LOG_LEVEL_ALWAYS;
|
|
|
|
}
|
|
|
|
else if (0 == g_strcasecmp(buf, "1") ||
|
|
|
|
0 == g_strcasecmp(buf, "error"))
|
|
|
|
{
|
|
|
|
return LOG_LEVEL_ERROR;
|
|
|
|
}
|
|
|
|
else if (0 == g_strcasecmp(buf, "2") ||
|
|
|
|
0 == g_strcasecmp(buf, "warn") ||
|
|
|
|
0 == g_strcasecmp(buf, "warning"))
|
|
|
|
{
|
|
|
|
return LOG_LEVEL_WARNING;
|
|
|
|
}
|
|
|
|
else if (0 == g_strcasecmp(buf, "3") ||
|
|
|
|
0 == g_strcasecmp(buf, "info"))
|
|
|
|
{
|
|
|
|
return LOG_LEVEL_INFO;
|
|
|
|
}
|
|
|
|
else if (0 == g_strcasecmp(buf, "4") ||
|
|
|
|
0 == g_strcasecmp(buf, "debug"))
|
|
|
|
{
|
|
|
|
return LOG_LEVEL_DEBUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_writeln("Your configured log level is corrupt - we use debug log level");
|
2012-05-27 19:17:39 +04:00
|
|
|
return LOG_LEVEL_DEBUG;
|
2005-10-24 01:50:42 +04:00
|
|
|
}
|
2012-05-27 19:17:39 +04:00
|
|
|
|
2012-06-04 23:51:22 +04:00
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internalReadConfiguration(const char *inFilename, const char *applicationName)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int fd;
|
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
|
|
|
struct list *sec;
|
|
|
|
struct list *param_n;
|
|
|
|
struct list *param_v;
|
|
|
|
|
|
|
|
if (inFilename == NULL)
|
|
|
|
{
|
|
|
|
g_writeln("The inifile is null to readConfiguration!");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = g_file_open(inFilename);
|
|
|
|
|
|
|
|
if (-1 == fd)
|
|
|
|
{
|
|
|
|
ret = LOG_ERROR_NO_CFG;
|
|
|
|
g_writeln("We could not open the configuration file to read log parameters");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we initialize the memory for the configuration and set all content
|
|
|
|
to zero. */
|
|
|
|
ret = internalInitAndAllocStruct();
|
|
|
|
|
|
|
|
if (ret != LOG_STARTUP_OK)
|
|
|
|
{
|
2014-07-19 22:56:00 +04:00
|
|
|
g_file_close(fd);
|
2012-09-20 07:51:34 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sec = list_create();
|
|
|
|
sec->auto_free = 1;
|
|
|
|
file_read_sections(fd, sec);
|
|
|
|
param_n = list_create();
|
|
|
|
param_n->auto_free = 1;
|
|
|
|
param_v = list_create();
|
|
|
|
param_v->auto_free = 1;
|
|
|
|
|
|
|
|
/* read logging config */
|
2014-07-26 09:42:07 +04:00
|
|
|
ret = internal_config_read_logging(fd, g_staticLogConfig, param_n,
|
2012-09-20 07:51:34 +04:00
|
|
|
param_v, applicationName);
|
|
|
|
|
|
|
|
if (ret != LOG_STARTUP_OK)
|
|
|
|
{
|
2014-07-19 22:56:00 +04:00
|
|
|
g_file_close(fd);
|
2012-09-20 07:51:34 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
list_delete(sec);
|
|
|
|
list_delete(param_v);
|
|
|
|
list_delete(param_n);
|
|
|
|
g_file_close(fd);
|
2012-06-04 23:51:22 +04:00
|
|
|
return ret;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
internal_config_read_logging(int file, struct log_config *lc,
|
|
|
|
struct list *param_n,
|
|
|
|
struct list *param_v,
|
|
|
|
const char *applicationName)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int i;
|
|
|
|
char *buf;
|
|
|
|
char *temp_buf;
|
|
|
|
|
|
|
|
list_clear(param_v);
|
|
|
|
list_clear(param_n);
|
|
|
|
|
|
|
|
/* setting defaults */
|
|
|
|
lc->program_name = g_strdup(applicationName);
|
|
|
|
lc->log_file = 0;
|
|
|
|
lc->fd = 0;
|
|
|
|
lc->log_level = LOG_LEVEL_DEBUG;
|
|
|
|
lc->enable_syslog = 0;
|
|
|
|
lc->syslog_level = LOG_LEVEL_DEBUG;
|
|
|
|
|
|
|
|
file_read_section(file, SESMAN_CFG_LOGGING, param_n, param_v);
|
|
|
|
|
|
|
|
for (i = 0; i < param_n->count; i++)
|
|
|
|
{
|
|
|
|
buf = (char *)list_get_item(param_n, i);
|
|
|
|
|
|
|
|
if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_FILE))
|
2012-06-11 08:50:57 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
lc->log_file = g_strdup((char *)list_get_item(param_v, i));
|
|
|
|
|
|
|
|
if (lc->log_file != NULL)
|
|
|
|
{
|
|
|
|
if (lc->log_file[0] != '/')
|
|
|
|
{
|
|
|
|
temp_buf = (char *)g_malloc(512, 0);
|
|
|
|
g_snprintf(temp_buf, 511, "%s/%s", XRDP_LOG_PATH, lc->log_file);
|
|
|
|
g_free(lc->log_file);
|
|
|
|
lc->log_file = temp_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_LEVEL))
|
|
|
|
{
|
|
|
|
lc->log_level = internal_log_text2level((char *)list_get_item(param_v, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_ENABLE_SYSLOG))
|
|
|
|
{
|
2013-08-25 09:41:44 +04:00
|
|
|
lc->enable_syslog = g_text2bool((char *)list_get_item(param_v, i));
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_SYSLOG_LEVEL))
|
|
|
|
{
|
|
|
|
lc->syslog_level = internal_log_text2level((char *)list_get_item(param_v, i));
|
2012-06-11 08:50:57 +04:00
|
|
|
}
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (0 == lc->log_file)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
lc->log_file = g_strdup("./sesman.log");
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
/* try to create path if not exist */
|
|
|
|
g_create_path(lc->log_file);
|
2012-05-27 19:17:39 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
g_printf("logging configuration:\r\n");
|
|
|
|
g_printf("\tLogFile: %s\r\n", lc->log_file);
|
|
|
|
g_printf("\tLogLevel: %i\r\n", lc->log_level);
|
|
|
|
g_printf("\tEnableSyslog: %i\r\n", lc->enable_syslog);
|
|
|
|
g_printf("\tSyslogLevel: %i\r\n", lc->syslog_level);
|
|
|
|
return LOG_STARTUP_OK;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 23:51:22 +04:00
|
|
|
enum logReturns DEFAULT_CC
|
|
|
|
internalInitAndAllocStruct(void)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
2014-07-26 09:42:07 +04:00
|
|
|
g_staticLogConfig = g_malloc(sizeof(struct log_config), 1);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig != NULL)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
g_staticLogConfig->fd = -1;
|
|
|
|
g_staticLogConfig->enable_syslog = 0;
|
2012-09-20 07:51:34 +04:00
|
|
|
ret = LOG_STARTUP_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_writeln("could not allocate memory for log struct");
|
|
|
|
ret = LOG_ERROR_MALLOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-06-04 23:51:22 +04:00
|
|
|
* Here below the public functions
|
2012-05-27 19:17:39 +04:00
|
|
|
*/
|
|
|
|
|
2012-06-04 23:51:22 +04:00
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
log_start_from_param(const struct log_config *iniParams)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig != NULL)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
log_message(LOG_LEVEL_ALWAYS, "Log already initialized");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iniParams == NULL)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
g_writeln("inparam to log_start_from_param is NULL");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*Copy the struct information*/
|
|
|
|
ret = internalInitAndAllocStruct();
|
|
|
|
|
|
|
|
if (ret != LOG_STARTUP_OK)
|
|
|
|
{
|
|
|
|
g_writeln("internalInitAndAllocStruct failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
g_staticLogConfig->enable_syslog = iniParams->enable_syslog;
|
|
|
|
g_staticLogConfig->fd = iniParams->fd;
|
|
|
|
g_staticLogConfig->log_file = g_strdup(iniParams->log_file);
|
|
|
|
g_staticLogConfig->log_level = iniParams->log_level;
|
|
|
|
g_staticLogConfig->log_lock = iniParams->log_lock;
|
|
|
|
g_staticLogConfig->log_lock_attr = iniParams->log_lock_attr;
|
|
|
|
g_staticLogConfig->program_name = g_strdup(iniParams->program_name);
|
|
|
|
g_staticLogConfig->syslog_level = iniParams->syslog_level;
|
|
|
|
ret = internal_log_start(g_staticLogConfig);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (ret != LOG_STARTUP_OK)
|
|
|
|
{
|
|
|
|
g_writeln("Could not start log");
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig != NULL)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
g_free(g_staticLogConfig);
|
|
|
|
g_staticLogConfig = NULL;
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
return ret;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-04 23:51:22 +04:00
|
|
|
* This function initialize the log facilities according to the configuration
|
2012-05-27 19:17:39 +04:00
|
|
|
* file, that is described by the in parameter.
|
|
|
|
* @param iniFile
|
|
|
|
* @param applicationName, the name that is used in the log for the running application
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
log_start(const char *iniFile, const char *applicationName)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
|
|
|
|
|
|
|
if (applicationName == NULL)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
g_writeln("Programming error your application name cannot be null");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = internalReadConfiguration(iniFile, applicationName);
|
|
|
|
|
|
|
|
if (ret == LOG_STARTUP_OK)
|
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
ret = internal_log_start(g_staticLogConfig);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (ret != LOG_STARTUP_OK)
|
|
|
|
{
|
|
|
|
g_writeln("Could not start log");
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig != NULL)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
g_free(g_staticLogConfig);
|
|
|
|
g_staticLogConfig = NULL;
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_writeln("Error reading configuration for log based on config: %s",
|
|
|
|
iniFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that terminates all logging
|
2012-06-04 23:51:22 +04:00
|
|
|
* @return
|
2012-05-27 19:17:39 +04:00
|
|
|
*/
|
|
|
|
enum logReturns DEFAULT_CC
|
2012-06-04 23:51:22 +04:00
|
|
|
log_end(void)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
enum logReturns ret = LOG_GENERAL_ERROR;
|
2014-07-26 09:42:07 +04:00
|
|
|
ret = internal_log_end(g_staticLogConfig);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig != NULL)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
g_free(g_staticLogConfig);
|
|
|
|
g_staticLogConfig = NULL;
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum logReturns DEFAULT_CC
|
2012-09-20 07:51:34 +04:00
|
|
|
log_message(const enum logLevels lvl, const char *msg, ...)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
char buff[LOG_BUFFER_SIZE + 31]; /* 19 (datetime) 4 (space+cr+lf+\0) */
|
|
|
|
va_list ap;
|
|
|
|
int len = 0;
|
|
|
|
enum logReturns rv = LOG_STARTUP_OK;
|
|
|
|
int writereply = 0;
|
|
|
|
time_t now_t;
|
|
|
|
struct tm *now;
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig == NULL)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
g_writeln("The log reference is NULL - log not initialized properly");
|
|
|
|
return LOG_ERROR_NO_CFG;
|
|
|
|
}
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (0 > g_staticLogConfig->fd && g_staticLogConfig->enable_syslog == 0)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
return LOG_ERROR_FILE_NOT_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
now_t = time(&now_t);
|
|
|
|
now = localtime(&now_t);
|
|
|
|
|
|
|
|
snprintf(buff, 21, "[%.4d%.2d%.2d-%.2d:%.2d:%.2d] ", now->tm_year + 1900,
|
|
|
|
now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min,
|
|
|
|
now->tm_sec);
|
|
|
|
|
|
|
|
internal_log_lvl2str(lvl, buff + 20);
|
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
len = vsnprintf(buff + 28, LOG_BUFFER_SIZE, msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* checking for truncated messages */
|
|
|
|
if (len > LOG_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
log_message(LOG_LEVEL_WARNING, "next message will be truncated");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* forcing the end of message string */
|
2012-06-04 23:51:22 +04:00
|
|
|
#ifdef _WIN32
|
2012-09-20 07:51:34 +04:00
|
|
|
buff[len + 28] = '\r';
|
|
|
|
buff[len + 29] = '\n';
|
|
|
|
buff[len + 30] = '\0';
|
2012-06-04 23:51:22 +04:00
|
|
|
#else
|
|
|
|
#ifdef _MACOS
|
2012-09-20 07:51:34 +04:00
|
|
|
buff[len + 28] = '\r';
|
|
|
|
buff[len + 29] = '\0';
|
2012-06-04 23:51:22 +04:00
|
|
|
#else
|
2012-09-20 07:51:34 +04:00
|
|
|
buff[len + 28] = '\n';
|
|
|
|
buff[len + 29] = '\0';
|
2012-06-04 23:51:22 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig->enable_syslog && (lvl <= g_staticLogConfig->syslog_level))
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
/* log to syslog*/
|
|
|
|
/* %s fix compiler warning 'not a string literal' */
|
|
|
|
syslog(internal_log_xrdp2syslog(lvl), "(%d)(%ld)%s", g_getpid(),
|
|
|
|
tc_get_threadid(), buff + 20);
|
|
|
|
}
|
2012-05-27 19:17:39 +04:00
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (lvl <= g_staticLogConfig->log_level)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
|
|
|
/* log to console */
|
2013-02-01 13:00:39 +04:00
|
|
|
g_printf("%s", buff);
|
2012-05-27 19:17:39 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
/* log to application logfile */
|
2012-05-27 19:17:39 +04:00
|
|
|
#ifdef LOG_ENABLE_THREAD
|
2014-07-26 09:42:07 +04:00
|
|
|
pthread_mutex_lock(&(g_staticLogConfig->log_lock));
|
2012-05-27 19:17:39 +04:00
|
|
|
#endif
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig->fd > 0)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
writereply = g_file_write(g_staticLogConfig->fd, buff, g_strlen(buff));
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (writereply <= 0)
|
|
|
|
{
|
|
|
|
rv = LOG_ERROR_NULL_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-27 19:17:39 +04:00
|
|
|
#ifdef LOG_ENABLE_THREAD
|
2014-07-26 09:42:07 +04:00
|
|
|
pthread_mutex_unlock(&(g_staticLogConfig->log_lock));
|
2012-05-27 19:17:39 +04:00
|
|
|
#endif
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the configured log file name
|
2012-06-04 23:51:22 +04:00
|
|
|
* @return
|
2012-05-27 19:17:39 +04:00
|
|
|
*/
|
2012-09-20 07:51:34 +04:00
|
|
|
char *DEFAULT_CC
|
|
|
|
getLogFile(char *replybuf, int bufsize)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig)
|
2012-05-27 19:17:39 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
if (g_staticLogConfig->log_file)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2014-07-26 09:42:07 +04:00
|
|
|
g_strncpy(replybuf, g_staticLogConfig->log_file, bufsize);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_sprintf(replybuf, "The log_file name is NULL");
|
|
|
|
}
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
g_snprintf(replybuf, bufsize, "The log is not properly started");
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
return replybuf;
|
2012-05-27 19:17:39 +04:00
|
|
|
}
|