Skip to content

Commit 946d870

Browse files
committed
Moved logging functins to their own module
1 parent 4cc220b commit 946d870

File tree

10 files changed

+143
-87
lines changed

10 files changed

+143
-87
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.8)
22

33
project(sol)
44
set (VERSION 0.17.1)
5+
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
56

67
OPTION(DEBUG "add debug flags" OFF)
78

@@ -13,7 +14,7 @@ set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
1314
include_directories(include)
1415
file(GLOB SOURCES src/*.c)
1516
file(GLOB TEST src/hashtable.c src/bst.c src/config.c src/list.c src/trie.c
16-
src/util.c src/iterator.c tests/*.c)
17+
src/util.c src/iterator.c src/logging.c tests/*.c)
1718

1819
set(AUTHOR "Andrea Giacomo Baldan")
1920
set(LICENSE "BSD2 license")

src/config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "util.h"
3636
#include "config.h"
3737
#include "network.h"
38+
#include "logging.h"
3839

3940
/* The main configuration structure */
4041
static struct config config;

src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifndef CONFIG_H
2929
#define CONFIG_H
3030

31+
#include <stdbool.h>
3132
#include "uthash.h"
3233

3334
// Eventloop backend check

src/handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
#include <stdio.h>
2929
#include "mqtt.h"
30-
#include "util.h"
3130
#include "config.h"
3231
#include "server.h"
32+
#include "logging.h"
3333
#include "sol_internal.h"
3434
#include "handlers.h"
3535

src/logging.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* BSD 2-Clause License
2+
*
3+
* Copyright (c) 2020, Andrea Giacomo Baldan All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
* POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#include <time.h>
29+
#include <stdio.h>
30+
#include <stdarg.h>
31+
#include <string.h>
32+
#include <assert.h>
33+
#include "logging.h"
34+
35+
#define MAX_LOG_SIZE 120
36+
37+
static FILE *fh = NULL;
38+
static int logging_level = DEBUG;
39+
40+
void sol_log_init(const char *file, int level) {
41+
if (!file) return;
42+
fh = fopen(file, "a+");
43+
logging_level = level;
44+
if (!fh)
45+
printf("%lu * WARNING: Unable to open file %s\n",
46+
(unsigned long) time(NULL), file);
47+
}
48+
49+
void sol_log_close(void) {
50+
if (fh) {
51+
fflush(fh);
52+
fclose(fh);
53+
}
54+
}
55+
56+
void sol_log(int level, const char *fmt, ...) {
57+
58+
if (level < logging_level)
59+
return;
60+
61+
assert(fmt);
62+
63+
va_list ap;
64+
char msg[MAX_LOG_SIZE + 4];
65+
66+
va_start(ap, fmt);
67+
vsnprintf(msg, sizeof(msg), fmt, ap);
68+
va_end(ap);
69+
70+
/* Truncate message too long and copy 3 bytes to make space for 3 dots */
71+
memcpy(msg + MAX_LOG_SIZE, "...", 3);
72+
msg[MAX_LOG_SIZE + 3] = '\0';
73+
74+
// Open two handler, one for standard output and a second for the
75+
// persistent log file
76+
FILE *fp = stdout;
77+
78+
if (!fp)
79+
return;
80+
81+
fprintf(fp, "%lu %s\n", (unsigned long) time(NULL), msg);
82+
if (fh)
83+
fprintf(fh, "%lu %s\n", (unsigned long) time(NULL), msg);
84+
}

src/logging.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* BSD 2-Clause License
2+
*
3+
* Copyright (c) 2020, Andrea Giacomo Baldan All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
* POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#ifndef LOGGING_H
29+
#define LOGGING_H
30+
31+
enum log_level { DEBUG, INFORMATION, WARNING, ERROR, FATAL };
32+
33+
void sol_log_init(const char *, int);
34+
void sol_log_close(void);
35+
void sol_log(int, const char *, ...);
36+
37+
#define log(...) sol_log( __VA_ARGS__ )
38+
#define log_debug(...) log(DEBUG, __VA_ARGS__)
39+
#define log_warning(...) log(WARNING, __VA_ARGS__)
40+
#define log_error(...) log(ERROR, __VA_ARGS__)
41+
#define log_info(...) log(INFORMATION, __VA_ARGS__)
42+
#define log_fatal(...) do { \
43+
log(FATAL, __VA_ARGS__); \
44+
exit(EXIT_FAILURE); \
45+
} while(0);
46+
47+
#endif

src/server.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
#include <unistd.h>
3131
#include <pthread.h>
3232
#include "network.h"
33-
#include "util.h"
3433
#include "config.h"
3534
#include "sol_internal.h"
3635
#include "server.h"
36+
#include "logging.h"
3737
#include "handlers.h"
3838
#include "memorypool.h"
3939
#include "ev.h"
@@ -966,8 +966,8 @@ int start_server(const char *addr, const char *port) {
966966
server.authentications = NULL;
967967
server.pool = memorypool_new(BASE_CLIENTS_NUM, sizeof(struct client));
968968
if (!server.pool)
969-
die("Failed to allocate %d sized memory pool for clients",
970-
BASE_CLIENTS_NUM);
969+
log_fatal("Failed to allocate %d sized memory pool for clients",
970+
BASE_CLIENTS_NUM);
971971
server.clients_map = NULL;
972972
server.sessions = NULL;
973973
server.wildcards = list_new(wildcard_destructor);

src/sol.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "util.h"
3434
#include "config.h"
3535
#include "server.h"
36+
#include "logging.h"
3637

3738
// Stops epoll_wait loops by sending an event
3839
static void sigint_handler(int signum) {
@@ -115,7 +116,7 @@ int main (int argc, char **argv) {
115116
// Try to load a configuration, if found
116117
config_load(confpath);
117118

118-
sol_log_init(conf->logpath);
119+
sol_log_init(conf->logpath, conf->loglevel);
119120

120121
if (daemon == 1)
121122
daemonize();

src/util.c

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* BSD 2-Clause License
22
*
3-
* Copyright (c) 2019, Andrea Giacomo Baldan All rights reserved.
3+
* Copyright (c) 2020, Andrea Giacomo Baldan All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
66
* modification, are permitted provided that the following conditions are met:
@@ -40,53 +40,6 @@
4040

4141
static atomic_size_t memory = ATOMIC_VAR_INIT(0);
4242

43-
static FILE *fh = NULL;
44-
45-
void sol_log_init(const char *file) {
46-
if (!file) return;
47-
fh = fopen(file, "a+");
48-
if (!fh)
49-
printf("%lu * WARNING: Unable to open file %s\n",
50-
(unsigned long) time(NULL), file);
51-
}
52-
53-
void sol_log_close(void) {
54-
if (fh) {
55-
fflush(fh);
56-
fclose(fh);
57-
}
58-
}
59-
60-
void sol_log(int level, const char *fmt, ...) {
61-
62-
if (level < conf->loglevel)
63-
return;
64-
65-
assert(fmt);
66-
67-
va_list ap;
68-
char msg[MAX_LOG_SIZE + 4];
69-
70-
va_start(ap, fmt);
71-
vsnprintf(msg, sizeof(msg), fmt, ap);
72-
va_end(ap);
73-
74-
/* Truncate message too long and copy 3 bytes to make space for 3 dots */
75-
memcpy(msg + MAX_LOG_SIZE, "...", 3);
76-
msg[MAX_LOG_SIZE + 3] = '\0';
77-
78-
// Open two handler, one for standard output and a second for the
79-
// persistent log file
80-
FILE *fp = stdout;
81-
82-
if (!fp)
83-
return;
84-
85-
fprintf(fp, "%lu %s\n", (unsigned long) time(NULL), msg);
86-
if (fh)
87-
fprintf(fh, "%lu %s\n", (unsigned long) time(NULL), msg);
88-
}
89-
9043
/* Auxiliary function to check wether a string is an integer */
9144
bool is_integer(const char *string) {
9245
for (; *string; ++string)
@@ -326,15 +279,3 @@ long get_fh_soft_limit(void) {
326279
}
327280
return limit.rlim_cur;
328281
}
329-
330-
void die(const char *msg, ...) {
331-
va_list ap;
332-
char error[MAX_LOG_SIZE];
333-
334-
va_start(ap, msg);
335-
vsnprintf(error, sizeof(error), msg, ap);
336-
va_end(ap);
337-
fprintf(stderr, "%s: Out of memory\n", error);
338-
fflush(stderr);
339-
abort();
340-
}

src/util.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* BSD 2-Clause License
22
*
3-
* Copyright (c) 2019, Andrea Giacomo Baldan All rights reserved.
3+
* Copyright (c) 2020, Andrea Giacomo Baldan All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
66
* modification, are permitted provided that the following conditions are met:
@@ -33,22 +33,13 @@
3333
#include <stdint.h>
3434
#include <stdbool.h>
3535

36-
#define MAX_LOG_SIZE 119
37-
3836
#define SOL_PREFIX "sol"
3937

40-
enum log_level { DEBUG, INFORMATION, WARNING, ERROR, FATAL };
41-
4238
bool is_integer(const char *);
4339
int parse_int(const char *);
4440
int number_len(size_t);
4541
void generate_random_id(char *);
4642

47-
/* Logging */
48-
void sol_log_init(const char *);
49-
void sol_log_close(void);
50-
void sol_log(int, const char *, ...);
51-
5243
/* Memory management */
5344
void *xmalloc(size_t);
5445
void *xcalloc(size_t, size_t);
@@ -59,22 +50,11 @@ char *xstrdup(const char *);
5950
char *remove_occur(char *, char);
6051
char *append_string(const char *, char *, size_t);
6152
bool check_passwd(const char *, const char *);
62-
void die(const char *, ...);
6353

6454
size_t memory_used(void);
6555

6656
long get_fh_soft_limit(void);
6757

68-
#define log(...) sol_log( __VA_ARGS__ )
69-
#define log_debug(...) log(DEBUG, __VA_ARGS__)
70-
#define log_warning(...) log(WARNING, __VA_ARGS__)
71-
#define log_error(...) log(ERROR, __VA_ARGS__)
72-
#define log_info(...) log(INFORMATION, __VA_ARGS__)
73-
#define log_fatal(...) do { \
74-
log(FATAL, __VA_ARGS__); \
75-
exit(EXIT_FAILURE); \
76-
} while(0);
77-
7858
#define STREQ(s1, s2, len) strncasecmp(s1, s2, len) == 0 ? true : false
7959

8060
#define container_of(ptr, type, field) \

0 commit comments

Comments
 (0)