AmendHub

Download: |

ftech

/

Logger

 

(View History)


A logging lib in C

Francois Techene   Updating LICENSE file from GPLv3 to MIT.

Name Last Commit Last Modified
LICENSE Updating LICENSE file from GPLv3 to MIT. 4 days ago
README v1.0 - Implemented console + refactored code to feature camel case syntax. 5 days ago
logger-test.c Updating license from GPLv3 to MIT. 4 days ago
logger.c Updating license from GPLv3 to MIT. 4 days ago
logger.h Updating license from GPLv3 to MIT. 4 days ago
test.log Updated app name + polished test app text. 5 days ago

README
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. It also requires the MacTraps libs if you plan to use the graphical console.
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.

---

# Usage

## Initializing the logger:

The logger can log to a graphical console that pops up at the bottom of the screen.
If you want to use the console, call the InitLogger() function with a log level and `CONSOLE_OUT` as the output method.

    InitLogger(DEBUG_LEVEL,
               CONSOLE_OUT,
               NULL);

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.

    InitLogger(DEBUG_LEVEL,
               FILE_OUT,
               ":file.log");

You can define paths as follow:

* ":file.log" - A simple file name relative to the project.
* "::logs:file.log" - Going back up 1 level and moving to the logs folder.
* "Macintosh HD:logs:file.log" - An absolute path.

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 if the log file doesn't exist, it is created but only if its containg folder exists.
If the file cannot be created, the output method is set to NONE_OUT. Nothing gets logged.
The log file is cleared every time InitLogger() is called.


Log output methods are as follow:
    NONE_OUT = 0, // Nothing gets logged. Fail safe internal use.
    CONSOLE_OUT = 1, // Log to the graphical console.
    FILE_OUT = 2 // Log to a file.
  
Log levels are as follow:
    NONE_LEVEL = 0,
    ERROR_LEVEL = 1,
    WARNING_LEVEL = 2,
    INFO_LEVEL = 3,
    DEBUG_LEVEL = 4,
  

InitLogger() initializes the global variable `logger`.


## Using the logger:

You can log some text by calling:

    logger.print(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.log(char* format, ...); // Same as print() but with a trailing new line.
    logger.debug(char* format, ...);
    logger.info(char* format, ...);
    logger.warn(char* format, ...);
    logger.error(char* format, ...);
    

## Using the console:

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.

You may place the following code at the top of your events handler function:

    if(logger.onEvent(event)) {
        return;
    }
  

## Customizing:

You can override the logger.print() function to handle logs the way you want, like for defining your own output method.

    void myLog(char* format, ...)
    {
        //Handle my log message.
    }
  
After initializing with no output, just set logger.print.

    InitLogger(DEBUG_LEVEL, NONE_OUT, NULL);
    logger.print = myLog;