summaryrefslogtreecommitdiffstats
path: root/common/log.h
blob: ac3b97c9815ee54789dbe679a55a1188c3290c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2005-2010
*/

#ifndef LOG_H
#define LOG_H

#include <pthread.h>

#include "arch.h"

/* logging buffer size */
#define LOG_BUFFER_SIZE      1024

/* logging levels */
enum logLevels{
  LOG_LEVEL_ALWAYS = 0,
  LOG_LEVEL_ERROR,
  LOG_LEVEL_WARNING,
  LOG_LEVEL_INFO,
  LOG_LEVEL_DEBUG
};

/* startup return values */
enum logReturns{
  LOG_STARTUP_OK = 0,
  LOG_ERROR_MALLOC,
  LOG_ERROR_NULL_FILE,
  LOG_ERROR_FILE_OPEN,
  LOG_ERROR_NO_CFG,
  LOG_ERROR_FILE_NOT_OPEN,
  LOG_GENERAL_ERROR
};

#define SESMAN_CFG_LOGGING           "Logging"
#define SESMAN_CFG_LOG_FILE          "LogFile"
#define SESMAN_CFG_LOG_LEVEL         "LogLevel"
#define SESMAN_CFG_LOG_ENABLE_SYSLOG "EnableSyslog"
#define SESMAN_CFG_LOG_SYSLOG_LEVEL  "SyslogLevel"

/* enable threading */
/*#define LOG_ENABLE_THREAD*/

#ifdef DEBUG
  #define LOG_DBG(args...) log_message(LOG_LEVEL_DEBUG, args);
#else
  #define LOG_DBG(args...)
#endif


struct log_config
{
  char* program_name;
  char* log_file;
  int fd;
  unsigned int log_level;
  int enable_syslog;
  unsigned int syslog_level;
  pthread_mutex_t log_lock;
  pthread_mutexattr_t log_lock_attr;
};

/* internal functions, only used in log.c if this ifdef is defined.*/
#ifdef LOGINTERNALSTUFF

/**
 *
 * @brief Starts the logging subsystem
 * @param l_cfg loggging system configuration
 * @return
 *
 */
enum logReturns DEFAULT_CC
internal_log_start(struct log_config* l_cfg);

/**
 *
 * @brief Shuts down the logging subsystem
 * @param l_cfg pointer to the logging subsystem to stop
 *
 */
enum logReturns DEFAULT_CC
internal_log_end(struct log_config* l_cfg);

/**
 * Converts a log level to a string
 * @param lvl, the loglevel 
 * @param str pointer where the string will be stored.
 */
void DEFAULT_CC
internal_log_lvl2str(const enum logLevels lvl, char* str);

/**
 *
 * @brief Converts a string to a log level
 * @param s The string to convert
 * @return The corresponding level or LOG_LEVEL_DEBUG if error
 *
 */
enum logLevels DEFAULT_CC
internal_log_text2level(char* s);

/**
 * A function that init our struct that holds all state and 
 * also init its content.
 * @return  LOG_STARTUP_OK or LOG_ERROR_MALLOC
 */
enum logReturns DEFAULT_CC
internalInitAndAllocStruct();

/**
 * Read configuration from a file and store the values in lists.
 * @param file
 * @param lc
 * @param param_n
 * @param param_v
 * @param applicationName, the application name used in the log events.
 * @return 
 */
enum logReturns DEFAULT_CC
internal_config_read_logging(int file, struct log_config* lc, struct list* param_n,
                    struct list* param_v,const char *applicationName) ;
/*End of internal functions*/
#endif
/**
 * This function initialize the log facilities according to the configuration 
 * 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 LOG_STARTUP_OK on success
 */
enum logReturns DEFAULT_CC 
log_start(const char *iniFile, const char *applicationName) ;

/**
 * An alternative log_start where the caller gives the params directly.
 * @param iniParams
 * @return 
 */
enum logReturns DEFAULT_CC
log_start_from_param(const struct log_config *iniParams);
/**
 * Function that terminates all logging
 * @return 
 */
enum logReturns DEFAULT_CC 
log_end() ;

/**
 * the log function that all files use to log an event.
 * @param lvl, the loglevel
 * @param msg, the logtext.
 * @param ...
 * @return 
 */
enum logReturns DEFAULT_CC 
log_message(const enum logLevels lvl, const char* msg, ...) ;

/**
 *
 * @brief Reads configuration
 * @param s translates the strings "1", "true" and "yes" in 1 (true) and 
 * other strings in 0
 * @return 0 on success, 1 on failure
 *
 */
int APP_CC text2bool(char* s);

/**
 * This function returns the configured file name for the logfile
 * @param replybuf the buffer where the reply is stored
 * @param bufsize how big is the reply buffer.
 * @return 
 */
char *getLogFile(char *replybuf, int bufsize);
#endif