summaryrefslogtreecommitdiff
path: root/src/logger.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/logger.c')
-rw-r--r--src/logger.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/logger.c b/src/logger.c
new file mode 100644
index 0000000..1060eca
--- /dev/null
+++ b/src/logger.c
@@ -0,0 +1,62 @@
+// GPGEdit edits GPG encrypted files
+// Copyright (C) 2018 Aaron Ball <nullspoon@oper.io>
+//
+// 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 <https://www.gnu.org/licenses/>.
+
+#include "logger.h"
+
+
+int _log_level = LOG_INFO; // Global log level, set by logger_init().
+
+
+// logger_init:
+// Initialize the logger-related variables.
+//
+// @lvl Integer representing log level. Use LOG_* macros (eg: LOG_ERROR,
+// LOG_DEBUG, LOG_INFO, etc)
+void logger_init(int lvl) {
+ _log_level = lvl;
+}
+
+
+// logger:
+// Logger function. Writes log messages to stdout if specified message level is
+// less than or equal to the runtime global log level. Global log level is set
+// by logger_init.
+//
+// @lvl Integer representing log level
+// @format Format (like printf) of message
+// @... Additional arguments for printf statement.
+void logger(int lvl, char* format, ...) {
+ va_list valist;
+ va_start(valist, format);
+
+ if(lvl <= _log_level) {
+ if(lvl == LOG_DEBUG)
+ printf("DEBUG: ");
+
+ if(lvl == LOG_FATAL)
+ printf("FATAL: ");
+
+ if(lvl == LOG_ERROR)
+ printf("ERROR: ");
+
+ if(lvl == LOG_ERROR)
+ printf("WARN: ");
+
+ vprintf(format, valist);
+ }
+
+ va_end(valist);
+}

Generated by cgit