source: appstream-generator/src/asgen/logging.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 2.0 KB
Line 
1/*
2 * Copyright (C) 2016 Matthias Klumpp <matthias@tenstral.net>
3 *
4 * Licensed under the GNU Lesser General Public License Version 3
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the license, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20module asgen.logging;
21
22import std.stdio;
23import std.string : format;
24import std.datetime;
25
26
27private __gshared bool __verbose = false;
28
29enum LogSeverity : string
30{
31    DEBUG = "DEBUG",
32    INFO = "INFO",
33    WARNING = "WARNING",
34    ERROR = "ERROR"
35}
36
37@trusted
38void logMessage (LogSeverity, string, Args...) (const LogSeverity severity, const string tmpl, const Args args)
39{
40    auto time = Clock.currTime ();
41    auto timeStr = "%d-%02d-%02d %02d:%02d:%02d".format (time.year, time.month, time.day, time.hour,time.minute, time.second);
42    writeln (timeStr, " - ", severity, ": ", format (tmpl, args));
43}
44
45@trusted
46void logDebug (string, Args...) (const string tmpl, const Args args)
47{
48    if (__verbose)
49        logMessage (LogSeverity.DEBUG, tmpl, args);
50}
51
52@safe
53void logInfo (string, Args...) (const string tmpl, const Args args)
54{
55    logMessage (LogSeverity.INFO, tmpl, args);
56}
57
58@safe
59void logWarning (string, Args...) (const string tmpl, const Args args)
60{
61    logMessage (LogSeverity.WARNING, tmpl, args);
62}
63
64@safe
65void logError (string, Args...) (const string tmpl, const Args args)
66{
67    logMessage (LogSeverity.ERROR, tmpl, args);
68}
69
70@trusted
71void setVerbose (const bool enabled)
72{
73    __verbose = enabled;
74}
Note: See TracBrowser for help on using the repository browser.