Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Global Message Reporter ( source code )
- * By Asha Dixon, 2018 */
- #include <stdio.h>
- #include <stdarg.h>
- #include "log_msg.h"
- #define LOG_LEVEL_MIN 0
- #define LOG_LEVEL_MAX 4
- /* log_prefixes: the prefixes to all the messages. This can
- * be indexed with the log_level enum ( see log_msg.h ) */
- char * log_prefixes[] = {
- "Error", "Internal Error", "Warning",
- "Information", "DEBUG"
- };
- /* log_message: outputs a message to the appropriate buffer with
- * the appropriate prefix. */
- void log_message ( enum log_level level, char * fmt, ... ) {
- va_list vaa;
- FILE * buffer = NULL;
- char * prefix = NULL;
- #ifndef DEBUG
- if ( level == LOG_LEVEL_DEBUG )
- return;
- #endif
- if ( level < LOG_LEVEL_MIN || level > LOG_LEVEL_MAX )
- return;
- // select buffer and prefix
- switch ( level ) {
- case LOG_LEVEL_ERROR:
- case LOG_LEVEL_INTERNAL:
- buffer = stderr;
- break;
- default:
- buffer = stdout;
- }
- prefix = log_prefixes[level];
- // output the message
- va_start ( vaa, fmt );
- fprintf ( buffer, "%s: ", prefix );
- vfprintf ( buffer, fmt, vaa );
- fprintf ( buffer, "\n" );
- va_end ( vaa );
- }
Add Comment
Please, Sign In to add comment