logging.c
Go to the documentation of this file.
00001 #include <sys/param.h>
00002 #include <stdarg.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <stdio.h>
00006 #include <assert.h>
00007 
00008 #include "logging.h"
00009 #include "csm_all.h"
00010 
00011 
00012 int sm_debug_write_flag = 0;
00013 
00014 const char * sm_program_name = 0;
00015 
00016 void sm_debug_write(int flag) {
00017         sm_debug_write_flag = flag;
00018 }
00019 
00020 char sm_program_name_temp[256];
00021 void sm_set_program_name(const char*name) {
00022         my_basename_no_suffix(name, sm_program_name_temp);
00023         sm_program_name = sm_program_name_temp;
00024 }
00025 
00026 int checked_for_xterm_color = 0;
00027 int xterm_color_available = 0;
00028 
00029 
00030 void check_for_xterm_color() {
00031         if(checked_for_xterm_color) return;
00032         checked_for_xterm_color = 1;
00033         
00034         const char * term = getenv("TERM");
00035         if(!term) term = "unavailable";
00036         xterm_color_available = !strcmp(term, "xterm-color") || !strcmp(term, "xterm") 
00037                 || !strcmp(term, "rxvt");
00038 /*      sm_info("Terminal type: '%s', colors: %d\n", term, xterm_color_available); */
00039 }
00040 
00041 #define XTERM_COLOR_RED "\e[1;37;41m"
00042 #define XTERM_COLOR_RESET "\e[0m"
00043 
00044 #define XTERM_ERROR XTERM_COLOR_RED
00045 #define XTERM_DEBUG "\e[1;35;40m"
00046 
00047 void sm_write_context();
00048 
00049 void sm_error(const char *msg, ...)
00050 {
00051         check_for_xterm_color();
00052         if(xterm_color_available)
00053                 fprintf(stderr, XTERM_ERROR);
00054                 
00055         if(sm_program_name) 
00056                 fprintf(stderr, "%s: ", sm_program_name);
00057         
00058         sm_write_context();
00059         
00060         if(!xterm_color_available) 
00061                 fputs(":err: ", stderr);
00062 
00063         va_list ap;
00064         va_start(ap, msg);
00065         vfprintf(stderr, msg, ap);
00066         
00067         if(xterm_color_available)
00068                 fprintf(stderr, XTERM_COLOR_RESET);
00069 }
00070 
00071 void sm_info(const char *msg, ...)
00072 {
00073         check_for_xterm_color();
00074         
00075         if(sm_program_name) 
00076                 fprintf(stderr, "%s: ", sm_program_name);
00077         
00078         sm_write_context();
00079         
00080         if(!xterm_color_available) 
00081                 fputs(":inf: ", stderr);
00082         
00083         va_list ap;
00084         va_start(ap, msg);
00085         vfprintf(stderr, msg, ap);
00086 }
00087 
00088 void sm_debug(const char *msg, ...)
00089 {
00090         if(!sm_debug_write_flag) return;
00091         
00092         check_for_xterm_color();
00093         
00094         if(xterm_color_available)
00095                 fprintf(stderr, XTERM_DEBUG);
00096         
00097         if(sm_program_name) 
00098                 fprintf(stderr, "%s: ", sm_program_name);
00099 
00100         sm_write_context();
00101 
00102         if(!xterm_color_available) 
00103                 fputs(":dbg: ", stderr);
00104         
00105         va_list ap;
00106         va_start(ap, msg);
00107         vfprintf(stderr, msg, ap);
00108         
00109         
00110         if(xterm_color_available)
00111                 fprintf(stderr, XTERM_COLOR_RESET);
00112 }
00113 
00114 #define MAX_CONTEXTS 1000
00115 const char * sm_log_context_name[MAX_CONTEXTS] = {""};
00116 int sm_log_context = 0;
00117 
00118 void sm_log_push(const char*cname) {
00119         if(sm_debug_write_flag) {
00120                 char message[1024]; sprintf(message, "  ___ %s \n", cname);
00121                 sm_debug(message);
00122         }
00123 
00124         assert(sm_log_context<MAX_CONTEXTS-1);
00125         sm_log_context++;
00126         sm_log_context_name[sm_log_context] = cname;
00127 }
00128 
00129 
00130 void sm_log_pop() {
00131         assert(sm_log_context>0);
00132         sm_log_context--;
00133 }
00134 
00135 void sm_write_context() {
00136         for(int i=0;i<sm_log_context;i++)
00137                 fprintf(stderr, "   ");
00138         
00139 }
00140 


csm
Author(s): Andrea Censi
autogenerated on Fri May 17 2019 02:28:33