File: log.c

package info (click to toggle)
frozen-bubble 2.212-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,852 kB
  • sloc: perl: 8,570; ansic: 1,200; sh: 92; makefile: 20
file content (116 lines) | stat: -rw-r--r-- 4,135 bytes parent folder | download | duplicates (3)
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
/*******************************************************************************
 *
 * Copyright (c) 2004 Guillaume Cottenceau
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ******************************************************************************/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>

#include "tools.h"
#include "log.h"
#include "net.h"

int output_type = OUTPUT_TYPE_INFO;
int debug_mode = FALSE;

time_t get_current_time(void) 
{
    struct timeval now;
    struct timezone tz;
    gettimeofday(&now, &tz);
    return now.tv_sec;
}

double get_current_time_exact(void) 
{
    struct timezone tz;
    struct timeval now;
    gettimeofday(&now, &tz);
    return (double) now.tv_sec + now.tv_usec / 1e6;  // bad bad idea to use float as precision is not down to the seconds then
}

char current_date[50];
char* get_current_date(void) 
{
    struct tm * lt;
    char buf[25];
    double time = get_current_time_exact();
    time_t seconds = (time_t)time;
    lt = localtime(&seconds);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt);
    snprintf(current_date, sizeof(current_date), "%s.%03d", buf, (int)(1000 * (time-seconds)));
    return current_date;
}

void logging_init(int portnum) {
        openlog(asprintf_("fb-server[TCP%d]", portnum), LOG_PID, LOG_DAEMON);
        if (output_type == OUTPUT_TYPE_DEBUG) {
                l0(OUTPUT_TYPE_INFO, "Starting log. Messages displayed: DEBUG, CONNECT, INFO, ERROR.");
        } else if (output_type == OUTPUT_TYPE_CONNECT) {
                l0(OUTPUT_TYPE_INFO, "Starting log. Messages displayed: CONNECT, INFO, ERROR.");
        } else if (output_type == OUTPUT_TYPE_INFO) {
                l0(OUTPUT_TYPE_INFO, "Starting log. Messages displayed: INFO, ERROR.");
        }
}

char* get_wanted_type(int wanted_output_type)
{
        return wanted_output_type == OUTPUT_TYPE_DEBUG ? "DEBUG"
             : wanted_output_type == OUTPUT_TYPE_CONNECT ? "CONNECT"
             : wanted_output_type == OUTPUT_TYPE_INFO ? "INFO"
             : "ERROR";
}

void l_(int wanted_output_type, char* file, long line, const char* func, char* fmt, ...)
{
    char *msg;
    va_list args;
    if (output_type <= wanted_output_type) {
            int level = 0;
            va_start(args, fmt);
            msg = vasprintf_(fmt, args); // segfault later if no more memory :)
            va_end(args);
            if (wanted_output_type == OUTPUT_TYPE_DEBUG) {
                    level = LOG_DEBUG;
            } else if (wanted_output_type == OUTPUT_TYPE_CONNECT) {
                    level = LOG_NOTICE;
            } else if (wanted_output_type == OUTPUT_TYPE_INFO) {
                    level = LOG_INFO;
            } else if (wanted_output_type == OUTPUT_TYPE_ERROR) {
                    level = LOG_ERR;
            }
            if (debug_mode)
                    fprintf(stderr, "[%s] %s %s:%ld(%s): %s\n", get_current_date(), get_wanted_type(wanted_output_type), file, line, func, msg);
            else {
                    if (output_type == OUTPUT_TYPE_DEBUG)
                            syslog(level, "[%s] %s %s:%ld(%s): %s", get_current_date(), get_wanted_type(wanted_output_type), file, line, func, msg);
                    else
                            syslog(level, "%s %s", get_wanted_type(wanted_output_type), msg);
            }
            free(msg);
    }
}