| 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. It also requires the MacTraps libs if you plan to use the graphical console. |
| 13 |
You may only use the graphical cosole if you are developing a graphical application and that all the Toolbox inits are made by your application. |
| 14 |
|
| 15 |
--- |
| 16 |
|
| 17 |
# Usage |
| 18 |
|
| 19 |
## Initializing the logger: |
| 20 |
|
| 21 |
The logger can log to a graphical console that pops up at the bottom of the screen. |
| 22 |
If you want to use the console, call the InitLogger() function with a log level and `CONSOLE_OUT` as the output method. |
| 23 |
|
| 24 |
InitLogger(DEBUG_LEVEL, |
| 25 |
CONSOLE_OUT, |
| 26 |
NULL); |
| 27 |
|
| 28 |
For the logger to log to a file, call the InitLogger() function with the FILE_OUT as the output method along with the path to the file to log to. |
| 29 |
|
| 30 |
InitLogger(DEBUG_LEVEL, |
| 31 |
FILE_OUT, |
| 32 |
":file.log"); |
| 33 |
|
| 34 |
You can define paths as follow: |
| 35 |
|
| 36 |
* ":file.log" - A simple file name relative to the project. |
| 37 |
* "::logs:file.log" - Going back up 1 level and moving to the logs folder. |
| 38 |
* "Macintosh HD:logs:file.log" - An absolute path. |
| 39 |
|
| 40 |
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. |
| 41 |
|
| 42 |
Note that if the log file doesn't exist, it is created but only if its containg folder exists. |
| 43 |
If the file cannot be created, the output method is set to NONE_OUT. Nothing gets logged. |
| 44 |
The log file is cleared every time InitLogger() is called. |
| 45 |
|
| 46 |
|
| 47 |
Log output methods are as follow: |
| 48 |
NONE_OUT = 0, // Nothing gets logged. Fail safe internal use. |
| 49 |
CONSOLE_OUT = 1, // Log to the graphical console. |
| 50 |
FILE_OUT = 2 // Log to a file. |
| 51 |
|
| 52 |
Log levels are as follow: |
| 53 |
NONE_LEVEL = 0, |
| 54 |
ERROR_LEVEL = 1, |
| 55 |
WARNING_LEVEL = 2, |
| 56 |
INFO_LEVEL = 3, |
| 57 |
DEBUG_LEVEL = 4, |
| 58 |
|
| 59 |
|
| 60 |
InitLogger() initializes the global variable `logger`. |
| 61 |
|
| 62 |
|
| 63 |
## Using the logger: |
| 64 |
|
| 65 |
You can log some text by calling: |
| 66 |
|
| 67 |
logger.print(char* format, ...); |
| 68 |
|
| 69 |
You can format your log message with variables: |
| 70 |
|
| 71 |
int count = 10; |
| 72 |
char* what = "students"; |
| 73 |
logger.log("There are %d %s", count, what); |
| 74 |
|
| 75 |
--> Will output: "There are 10 students". |
| 76 |
|
| 77 |
You can also use the following methods to differentiate debug, warning and error messages from your log output. |
| 78 |
|
| 79 |
logger.log(char* format, ...); // Same as print() but with a trailing new line. |
| 80 |
logger.debug(char* format, ...); |
| 81 |
logger.info(char* format, ...); |
| 82 |
logger.warn(char* format, ...); |
| 83 |
logger.error(char* format, ...); |
| 84 |
|
| 85 |
|
| 86 |
## Using the console: |
| 87 |
|
| 88 |
If you are logging to the console, you need to forward the application's events to the logger and ignore the event if it has been handled by the console. |
| 89 |
|
| 90 |
You may place the following code at the top of your events handler function: |
| 91 |
|
| 92 |
if(logger.onEvent(event)) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
## Customizing: |
| 98 |
|
| 99 |
You can override the logger.print() function to handle logs the way you want, like for defining your own output method. |
| 100 |
|
| 101 |
void myLog(char* format, ...) |
| 102 |
{ |
| 103 |
//Handle my log message. |
| 104 |
} |
| 105 |
|
| 106 |
After initializing with no output, just set logger.print. |
| 107 |
|
| 108 |
InitLogger(DEBUG_LEVEL, NONE_OUT, NULL); |
| 109 |
logger.print = myLog; |