ftech
/Logger
/amendments
/1
First commit
Francois Techene made amendment 1 11 days ago
--- LICENSE Mon Apr 7 12:04:33 2025
+++ LICENSE Mon Apr 7 12:04:33 2025
@@ -0,0 +1,7 @@
+Copyright (c) 2025 Francois Techene <ftechene@yahoo.fr>
+
+The Logger Library for classic Mac 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.
+
+<https://www.gnu.org/licenses/gpl-3.0.en.html>.
--- logger-test.c Mon Apr 7 10:51:06 2025
+++ logger-test.c Mon Apr 7 11:26:12 2025
@@ -0,0 +1,96 @@
+/* logger_test.c
+ *
+ * Copyright 2025 Francois Techene
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "logger.h"
+
+int
+main(void)
+{
+
+ printf("+-----------------------------------+\n");
+ printf("| Logger Test |\n");
+ printf("+-----------------------------------+\n\n");
+
+ // You can use a file in a shared folder on the network
+ // and use "tail -f" on the remote machine the see the logs
+ // Comment the next init_log if you want to use it.
+ //
+ init_logger(DEBUG_LEVEL, FILE_OUT, "Shared:Mac Dev:Logger:test.log");
+
+
+ // This path is relative to the project's folder
+ //
+ init_logger(DEBUG_LEVEL, FILE_OUT, "test.log");
+
+
+ printf("\n-- Logging to file: %s\n", logger.file_path);
+
+ logger.debug("This is a debug message to %s", logger.file_path);
+ logger.warn("This is a warning to %s", logger.file_path);
+ logger.error("This is an error to %s", logger.file_path);
+
+
+ init_logger(DEBUG_LEVEL, CONSOLE_OUT, NULL);
+
+ printf("\n-- Logging to the console:\n\n");
+ logger.debug("This is a debug message with number %d ", 1);
+ logger.warn("This is a warning with number %d ", 2);
+ logger.error("This is an error with number %d ", 3);
+
+
+ init_logger(NONE_LEVEL, CONSOLE_OUT, NULL);
+
+ printf("\n-- NONE_LEVEL: Must not show any log: \n\n");
+ logger.debug("This debug log should not be visible");
+ logger.warn("This warning log should not be visible");
+ logger.error("This error log should not be visible");
+
+
+ init_logger(ERROR_LEVEL, CONSOLE_OUT, NULL);
+
+ printf("\n-- ERROR_LEVEL: Must show only errors: \n\n");
+ logger.debug("This debug log should not be visible");
+ logger.warn("This warning log should not be visible");
+ logger.error("This error log should be visible");
+
+
+ init_logger(WARNING_LEVEL, CONSOLE_OUT, NULL);
+
+ printf("\n-- WARNING_LEVEL: Must show errors and warnings: \n\n");
+ logger.debug("This debug log should not be visible");
+ logger.warn("This warning log should be visible");
+ logger.error("This error log should be visible");
+
+
+ init_logger(DEBUG_LEVEL, NONE_OUT, NULL);
+
+ printf("\n-- Logging to none output: \n\n");
+ logger.debug("This debug log should not be visible");
+ logger.warn("This warning log should not be visible");
+ logger.error("This error log should not be visible");
+
+ printf("\nDone!\n");
+
+
+ return 1;
+}
--- logger.c Mon Apr 7 10:50:21 2025
+++ logger.c Mon Apr 7 11:24:55 2025
@@ -0,0 +1,156 @@
+/* logger.c
+ *
+ * Copyright 2025 Francois Techene
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#include "logger.h"
+
+Logger logger;
+
+void
+init_logger(LogLevel level,
+ LogOutputMethod out,
+ char* file_path)
+{
+ logger.debug = log_debug;
+ logger.warn = log_warn;
+ logger.error = log_error;
+
+ logger.log_level = level;
+ logger.output_method = out;
+ logger.file_path = file_path;
+
+ if (out == CONSOLE_OUT) {
+ logger.log = log_to_console;
+ }
+ else if (out == FILE_OUT) {
+ FILE* file;
+
+ logger.log = log_to_file;
+
+ // Empty log file
+ //
+ file = fopen(logger.file_path, "w");
+ if(!file) {
+ return;
+ }
+ fprintf(file, "");
+ fclose(file);
+ }
+ else {
+ logger.log = log_to_none;
+ }
+}
+
+void
+log_to_none(char* format, ...)
+{
+ return;
+}
+
+void
+log_to_console(char* format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vprintf(format, ap);
+ va_end(ap);
+
+ printf("\n");
+}
+void
+log_to_file(char* format, ...)
+{
+ FILE* file;
+ va_list ap;
+
+ // Open file in Append Binary mode so the
+ // new line character is not converted.
+ //
+ file = fopen(logger.file_path, "ab");
+ if(!file) {
+ return;
+ }
+
+ va_start(ap, format);
+ vfprintf(file, format, ap);
+ va_end(ap);
+
+ // Using Unix New Line character for tail to
+ // read correctly on Linux.
+ //
+ fprintf(file, " \x0A\r");
+
+ fclose(file);
+}
+
+void
+log_debug(char* format, ...)
+{
+ va_list ap;
+ char log[256] = "";
+
+ if (logger.log_level < DEBUG_LEVEL) {
+ return;
+ }
+
+ va_start(ap, format);
+ vsprintf(log, format, ap);
+ va_end(ap);
+
+ logger.log("DEBUG: %s", log);
+}
+
+void
+log_warn(char* format, ...)
+{
+ va_list ap;
+ char log[256] = "";
+
+ if (logger.log_level < WARNING_LEVEL) {
+ return;
+ }
+
+ va_start(ap, format);
+ vsprintf(log, format, ap);
+ va_end(ap);
+
+ logger.log("WARNING: %s", log);
+}
+
+void
+log_error(char* format, ...)
+{
+ va_list ap;
+ char log[256] = "";
+
+ if (logger.log_level < ERROR_LEVEL) {
+ return;
+ }
+
+ va_start(ap, format);
+ vsprintf(log, format, ap);
+ va_end(ap);
+
+ logger.log("ERROR: %s", log);
+}
--- logger.h Mon Apr 7 10:40:19 2025
+++ logger.h Mon Apr 7 11:25:44 2025
@@ -0,0 +1,69 @@
+/* logger.h
+ *
+ * Copyright 2025 Francois Techene
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#ifndef LOGGER_H
+#define LOGGER_H
+
+typedef enum {
+ NONE_LEVEL = 0,
+ ERROR_LEVEL,
+ WARNING_LEVEL,
+ DEBUG_LEVEL
+} LogLevel;
+
+typedef enum {
+ NONE_OUT = 0,
+ CONSOLE_OUT,
+ FILE_OUT
+
+} LogOutputMethod;
+
+
+
+typedef struct _Logger {
+
+ void (*log) (char* format, ...);
+ void (*debug) (char* format, ...);
+ void (*warn) (char* format, ...);
+ void (*error) (char* format, ...);
+
+ char* file_path;
+ int output_method;
+ int log_level;
+
+
+} Logger;
+
+extern Logger logger;
+
+void init_logger(LogLevel level,
+ LogOutputMethod out,
+ char* file_path);
+
+void log_debug(char* format, ...);
+void log_warn(char* format, ...);
+void log_error(char* format, ...);
+
+void log_to_none(char* format, ...);
+void log_to_console(char* format, ...);
+void log_to_file(char* format, ...);
+
+
+#endif
--- README Mon Apr 7 12:03:00 2025
+++ README Mon Apr 7 12:03:00 2025
@@ -0,0 +1,88 @@
+Logger is a C library that implements a simple logging system for classic Macintosh programs.
+
+Logger is free software; see the LICENSE file for copyright/licensing
+---
+
+# Install
+
+This library has been built and tested using THINK C v5.0 on a Macintosh SE.
+
+In order to install it, just import `logger.c` and `logger.h` to your project and include `logger.h` in your source files.
+
+The Logger library requires ANSI to be added to your project.
+
+---
+
+# Usage
+
+## Initializing the logger:
+
+For the logger to log to the console (through printf()), call the init_logger() function with a log level and `CONSOLE_OUT` as the output method.
+
+ init_logger(ERROR_LEVEL,
+ CONSOLE_OUT,
+ NULL);
+
+For the logger to log to a file, call the init_logger() function with the FILE_OUT as the output method along with the path to the file to log to.
+
+ init_logger(DEBUG_LEVEL,
+ FILE_OUT,
+ "file.log");
+
+A simple file name like "file.log" will log to a file in the current project folder.
+You can use a full path as follow: "Macintosh HD:Logs:my_project.log".
+
+Tip: The log file can be stored on a network volume (from Netatalk) and loaded with `tail -f` on a modern machine so that logs are shown in real time.
+
+Note that the log file is not created. It needs to exist on the drive. It is cleared every time init_logger() is called.
+
+
+Log output methods are as follow:
+ NONE_OUT = 0,
+ CONSOLE_OUT = 1,
+ FILE_OUT = 2,
+
+Log levels are as follow:
+ NONE_LEVEL = 0,
+ ERROR_LEVEL = 1,
+ WARNING_LEVEL = 2,
+ DEBUG_LEVEL = 3,
+
+
+init_logger() initializes the global variable `logger`.
+
+
+## Using the logger:
+
+You can log a new line by calling:
+
+ logger.log(char* format, ...);
+
+You can format your log message with variables:
+
+ int count = 10;
+ char* what = "students";
+ logger.log("There are %d %s", count, what);
+
+Will output: "There are 10 students".
+
+You can also use the following methods to differentiate debug, warning and error messages from your log output.
+
+ logger.debug(char* format, ...);
+ logger.warn(char* format, ...);
+ logger.error(char* format, ...);
+
+
+## Customizing:
+
+You can override the logger.log() function to handle logs the way you want, like printing the message in a text area on the screen.
+
+ void my_log(char* format, ...)
+ {
+ //Handle my log message.
+ }
+
+After initializing with no output, just set logger.log.
+
+ init_logger(DEBUG_LEVEL, NONE_OUT, NULL);
+ logger.log = my_log;
--- test.log Mon Apr 7 10:58:39 2025
+++ test.log Mon Apr 7 11:26:35 2025
@@ -0,0 +1,3 @@
+DEBUG: This is a debug message to test.log
+WARNING: This is a warning to test.log
+ERROR: This is an error to test.log