summaryrefslogtreecommitdiff
path: root/src/utilities/logger.vala
blob: 7c666155debf0efd6186786ad1797864b6f5d015 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2015 by Simon Schneegans
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// A static class which beautifies the messages of the default logger.
/// Some of this code is inspired by plank's written by Robert Dyer.
/// Thanks a lot for this project!
/////////////////////////////////////////////////////////////////////////

public class Logger {

    /////////////////////////////////////////////////////////////////////
    /// If these are set to false, the according messages are not shown
    /////////////////////////////////////////////////////////////////////

    private static const bool display_debug = true;
    private static const bool display_warning = true;
    private static const bool display_error = true;
    private static const bool display_message = true;

    /////////////////////////////////////////////////////////////////////
    /// If these are set to false, the according messages are not logged
    /////////////////////////////////////////////////////////////////////

    private static const bool log_debug = false;
    private static const bool log_warning = true;
    private static const bool log_error = true;
    private static const bool log_message = true;

    /////////////////////////////////////////////////////////////////////
    /// If true, a time stamp is shown in each message.
    /////////////////////////////////////////////////////////////////////

    private static const bool display_time = false;
    private static const bool log_time = true;

    /////////////////////////////////////////////////////////////////////
    /// If true, the origin of the message is shown. In form file:line
    /////////////////////////////////////////////////////////////////////

    private static const bool display_file = false;
    private static const bool log_file = false;

    /////////////////////////////////////////////////////////////////////
    /// A regex, used to format the standard message.
    /////////////////////////////////////////////////////////////////////

    private static Regex regex = null;

    /////////////////////////////////////////////////////////////////////
    /// Limit log and statistics size to roughly 1 MB.
    /////////////////////////////////////////////////////////////////////

    private static const int max_log_length = 1000000;

    private static int log_length;

    /////////////////////////////////////////////////////////////////////
    /// Possible terminal colors.
    /////////////////////////////////////////////////////////////////////

    private enum Color {
        BLACK,
        RED,
        GREEN,
        YELLOW,
        BLUE,
        PURPLE,
        TURQUOISE,
        WHITE
    }

    /////////////////////////////////////////////////////////////////////
    /// Creates the regex and binds the handler.
    /////////////////////////////////////////////////////////////////////

    public static void init() {
        log_length = -1;

        try {
            regex = new Regex("""(.*)\.vala(:\d+): (.*)""");
        } catch {}

        GLib.Log.set_handler(null, GLib.LogLevelFlags.LEVEL_MASK, log_func);
    }

    /////////////////////////////////////////////////////////////////////
    /// Appends a line to the log file
    /////////////////////////////////////////////////////////////////////

    private static void write_log_line(string line) {
        var log = GLib.FileStream.open(Paths.log, "a");

        if (log != null) {
            if (log_length == -1)
                log_length = (int)log.tell();

            log.puts(line);
            log_length += line.length;
        }

        if (log_length > max_log_length) {
            string content = "";

            try {
                GLib.FileUtils.get_contents(Paths.log, out content);
                int split_index = content.index_of_char('\n', log_length - (int)(max_log_length*0.9));
                GLib.FileUtils.set_contents(Paths.log, content.substring(split_index+1));

                log_length -= (split_index+1);
            } catch (GLib.FileError e) {}
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Displays a message.
    /////////////////////////////////////////////////////////////////////

    private static void message(string message, string message_log) {
        if (display_message) {
            stdout.printf(set_color(Color.GREEN, false) + "[" + (display_time ? get_time() + " " : "") + "MESSAGE]" + message);
        }

        if (log_message) {
            write_log_line("[" + (log_time ? get_time() + " " : "") + "MESSAGE]" + message_log);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Displays a Debug message.
    /////////////////////////////////////////////////////////////////////

    private static void debug(string message, string message_log) {
        if (display_debug) {
            stdout.printf(set_color(Color.BLUE, false) + "[" + (display_time ? get_time() + " " : "") + " DEBUG ]" + message);
        }

        if (log_debug) {
            write_log_line("[" + (log_time ? get_time() + " " : "") + " DEBUG ]" + message_log);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Displays a Warning message.
    /////////////////////////////////////////////////////////////////////

    private static void warning(string message, string message_log) {
        if (display_warning) {
            stdout.printf(set_color(Color.YELLOW, false) + "[" + (display_time ? get_time() + " " : "") + "WARNING]" + message);
        }

        if (log_warning) {
            write_log_line("[" + (log_time ? get_time() + " " : "") + "WARNING]" + message_log);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Displays a Error message.
    /////////////////////////////////////////////////////////////////////

    private static void error(string message, string message_log) {
        if (display_error) {
            stdout.printf(set_color(Color.RED, false) + "[" + (display_time ? get_time() + " " : "") + " ERROR ]" + message);
        }

        if (log_error) {
            write_log_line("[" + (log_time ? get_time() + " " : "") + " ERROR ]" + message_log);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Helper method which resets the terminal color.
    /////////////////////////////////////////////////////////////////////

    private static string reset_color() {
        return "\x001b[0m";
    }

    /////////////////////////////////////////////////////////////////////
    /// Helper method which sets the terminal color.
    /////////////////////////////////////////////////////////////////////

    private static string set_color(Color color, bool bold) {
        if (bold) return "\x001b[1;%dm".printf((int)color + 30);
        else      return "\x001b[0;%dm".printf((int)color + 30);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the current time in hh:mm:ss:mmmmmm
    /////////////////////////////////////////////////////////////////////

    private static string get_time() {
        var now = new DateTime.now_local();
        return "%.4d:%.2d:%.2d:%.2d:%.2d:%.2d:%.6d".printf(now.get_year(), now.get_month(), now.get_day_of_month(), now.get_hour(), now.get_minute(), now.get_second(), now.get_microsecond());
    }

    /////////////////////////////////////////////////////////////////////
    /// Helper method to format the message.
    /////////////////////////////////////////////////////////////////////

    private static string create_message(string message) {
        if (display_file && regex != null && regex.match(message)) {
            var parts = regex.split(message);
            return " [%s%s]%s %s\n".printf(parts[1], parts[2], reset_color(), parts[3]);
        } else if (regex != null && regex.match(message)) {
            var parts = regex.split(message);
            return "%s %s\n".printf(reset_color(), parts[3]);
        } else {
            return reset_color() + " " + message + "\n";
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Helper method to format the message for logging.
    /////////////////////////////////////////////////////////////////////

    private static string create_log_message(string message) {
        if (log_file && regex != null && regex.match(message)) {
            var parts = regex.split(message);
            return " [%s%s] %s\n".printf(parts[1], parts[2], parts[3]);
        } else if (regex != null && regex.match(message)) {
            var parts = regex.split(message);
            return " %s\n".printf(parts[3]);
        } else {
            return " " + message + "\n";
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// The handler function.
    /////////////////////////////////////////////////////////////////////

    private static void log_func(string? d, LogLevelFlags flags, string text) {
        switch (flags) {
            case LogLevelFlags.LEVEL_ERROR:
            case LogLevelFlags.LEVEL_CRITICAL:
                error(create_message(text), create_log_message(text));
                break;
            case LogLevelFlags.LEVEL_INFO:
            case LogLevelFlags.LEVEL_MESSAGE:
                message(create_message(text), create_log_message(text));
                break;
            case LogLevelFlags.LEVEL_DEBUG:
                debug(create_message(text), create_log_message(text));
                break;
            case LogLevelFlags.LEVEL_WARNING:
            default:
                warning(create_message(text), create_log_message(text));
                break;
        }
    }
}

}