Guest User

Untitled

a guest
Jul 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. /* Global Message Reporter ( source code )
  2. * By Asha Dixon, 2018 */
  3.  
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include "log_msg.h"
  7.  
  8. #define LOG_LEVEL_MIN 0
  9. #define LOG_LEVEL_MAX 4
  10.  
  11. /* log_prefixes: the prefixes to all the messages. This can
  12. * be indexed with the log_level enum ( see log_msg.h ) */
  13. char * log_prefixes[] = {
  14. "Error", "Internal Error", "Warning",
  15. "Information", "DEBUG"
  16. };
  17.  
  18. /* log_message: outputs a message to the appropriate buffer with
  19. * the appropriate prefix. */
  20. void log_message ( enum log_level level, char * fmt, ... ) {
  21. va_list vaa;
  22. FILE * buffer = NULL;
  23. char * prefix = NULL;
  24.  
  25. #ifndef DEBUG
  26. if ( level == LOG_LEVEL_DEBUG )
  27. return;
  28. #endif
  29.  
  30. if ( level < LOG_LEVEL_MIN || level > LOG_LEVEL_MAX )
  31. return;
  32.  
  33. // select buffer and prefix
  34.  
  35. switch ( level ) {
  36. case LOG_LEVEL_ERROR:
  37. case LOG_LEVEL_INTERNAL:
  38. buffer = stderr;
  39. break;
  40. default:
  41. buffer = stdout;
  42. }
  43.  
  44. prefix = log_prefixes[level];
  45.  
  46. // output the message
  47.  
  48. va_start ( vaa, fmt );
  49.  
  50. fprintf ( buffer, "%s: ", prefix );
  51. vfprintf ( buffer, fmt, vaa );
  52. fprintf ( buffer, "\n" );
  53.  
  54. va_end ( vaa );
  55. }
Add Comment
Please, Sign In to add comment