Download
ftech
/Logger
/README
(View History)
Francois Techene Updated README | Latest amendment: 5 on 2025-04-08 |
1 | Logger is a C library that implements a simple logging system for classic Macintosh programs. |
2 | |
3 | Logger is free software; see the LICENSE file for copyright/licensing |
4 | --- |
5 | |
6 | # Install |
7 | |
8 | This library has been built and tested using THINK C v5.0 on a Macintosh SE. |
9 | |
10 | In order to install it, just import `logger.c` and `logger.h` to your project and include `logger.h` in your source files. |
11 | |
12 | The Logger library requires ANSI to be added to your project. |
13 | |
14 | --- |
15 | |
16 | # Usage |
17 | |
18 | ## Initializing the logger: |
19 | |
20 | 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. |
21 | |
22 | init_logger(ERROR_LEVEL, |
23 | CONSOLE_OUT, |
24 | NULL); |
25 | |
26 | 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. |
27 | |
28 | init_logger(DEBUG_LEVEL, |
29 | FILE_OUT, |
30 | "file.log"); |
31 | |
32 | You can define paths as follow: |
33 | |
34 | * "file.log" - A simple file name relative to the project. |
35 | * ":logs:file.log" - An path relative to the project. |
36 | * "Macintosh HD:logs:file.log" - An absolute path. |
37 | |
38 | 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. |
39 | |
40 | Note that if the log file doesn't exist, it is created but only if its containg folder exists. |
41 | If the file cannot be created, the output method is set to NONE_OUT. |
42 | The log file is cleared every time init_logger() is called. |
43 | |
44 | |
45 | Log output methods are as follow: |
46 | NONE_OUT = 0, |
47 | CONSOLE_OUT = 1, |
48 | FILE_OUT = 2, |
49 | |
50 | Log levels are as follow: |
51 | NONE_LEVEL = 0, |
52 | ERROR_LEVEL = 1, |
53 | WARNING_LEVEL = 2, |
54 | INFO_LEVEL = 3, |
55 | DEBUG_LEVEL = 4, |
56 | |
57 | |
58 | init_logger() initializes the global variable `logger`. |
59 | |
60 | |
61 | ## Using the logger: |
62 | |
63 | You can log a new line by calling: |
64 | |
65 | logger.log(char* format, ...); |
66 | |
67 | You can format your log message with variables: |
68 | |
69 | int count = 10; |
70 | char* what = "students"; |
71 | logger.log("There are %d %s", count, what); |
72 | |
73 | --> Will output: "There are 10 students". |
74 | |
75 | You can also use the following methods to differentiate debug, warning and error messages from your log output. |
76 | |
77 | logger.debug(char* format, ...); |
78 | logger.info(char* format, ...); |
79 | logger.warn(char* format, ...); |
80 | logger.error(char* format, ...); |
81 | |
82 | |
83 | ## Customizing: |
84 | |
85 | 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. |
86 | |
87 | void my_log(char* format, ...) |
88 | { |
89 | //Handle my log message. |
90 | } |
91 | |
92 | After initializing with no output, just set logger.log. |
93 | |
94 | init_logger(DEBUG_LEVEL, NONE_OUT, NULL); |
95 | logger.log = my_log; |