logging.c
Go to the documentation of this file.
1 #include <sys/param.h>
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <assert.h>
7 
8 #include "logging.h"
9 #include "csm_all.h"
10 
11 
13 
14 const char * sm_program_name = 0;
15 
16 void sm_debug_write(int flag) {
17  sm_debug_write_flag = flag;
18 }
19 
21 void sm_set_program_name(const char*name) {
24 }
25 
28 
29 
31  if(checked_for_xterm_color) return;
33 
34  const char * term = getenv("TERM");
35  if(!term) term = "unavailable";
36  xterm_color_available = !strcmp(term, "xterm-color") || !strcmp(term, "xterm")
37  || !strcmp(term, "rxvt");
38 /* sm_info("Terminal type: '%s', colors: %d\n", term, xterm_color_available); */
39 }
40 
41 #define XTERM_COLOR_RED "\e[1;37;41m"
42 #define XTERM_COLOR_RESET "\e[0m"
43 
44 #define XTERM_ERROR XTERM_COLOR_RED
45 #define XTERM_DEBUG "\e[1;35;40m"
46 
47 void sm_write_context();
48 
49 void sm_error(const char *msg, ...)
50 {
53  fprintf(stderr, XTERM_ERROR);
54 
55  if(sm_program_name)
56  fprintf(stderr, "%s: ", sm_program_name);
57 
59 
61  fputs(":err: ", stderr);
62 
63  va_list ap;
64  va_start(ap, msg);
65  vfprintf(stderr, msg, ap);
66 
68  fprintf(stderr, XTERM_COLOR_RESET);
69 }
70 
71 void sm_info(const char *msg, ...)
72 {
74 
75  if(sm_program_name)
76  fprintf(stderr, "%s: ", sm_program_name);
77 
79 
81  fputs(":inf: ", stderr);
82 
83  va_list ap;
84  va_start(ap, msg);
85  vfprintf(stderr, msg, ap);
86 }
87 
88 void sm_debug(const char *msg, ...)
89 {
90  if(!sm_debug_write_flag) return;
91 
93 
95  fprintf(stderr, XTERM_DEBUG);
96 
97  if(sm_program_name)
98  fprintf(stderr, "%s: ", sm_program_name);
99 
101 
103  fputs(":dbg: ", stderr);
104 
105  va_list ap;
106  va_start(ap, msg);
107  vfprintf(stderr, msg, ap);
108 
109 
111  fprintf(stderr, XTERM_COLOR_RESET);
112 }
113 
114 #define MAX_CONTEXTS 1000
115 const char * sm_log_context_name[MAX_CONTEXTS] = {""};
117 
118 void sm_log_push(const char*cname) {
119  if(sm_debug_write_flag) {
120  char message[1024]; sprintf(message, " ___ %s \n", cname);
121  sm_debug(message);
122  }
123 
124  assert(sm_log_context<MAX_CONTEXTS-1);
125  sm_log_context++;
127 }
128 
129 
130 void sm_log_pop() {
131  assert(sm_log_context>0);
132  sm_log_context--;
133 }
134 
136  for(int i=0;i<sm_log_context;i++)
137  fprintf(stderr, " ");
138 
139 }
140 
void sm_log_push(const char *cname)
Definition: logging.c:118
void sm_set_program_name(const char *name)
Definition: logging.c:21
void check_for_xterm_color()
Definition: logging.c:30
const char * sm_log_context_name[MAX_CONTEXTS]
Definition: logging.c:115
char sm_program_name_temp[256]
Definition: logging.c:20
int sm_log_context
Definition: logging.c:116
#define XTERM_COLOR_RESET
Definition: logging.c:42
void my_basename_no_suffix(const char *file, char *dest)
Definition: utils.c:40
const char * sm_program_name
Definition: logging.c:14
#define XTERM_ERROR
Definition: logging.c:44
#define XTERM_DEBUG
Definition: logging.c:45
int checked_for_xterm_color
Definition: logging.c:26
int sm_debug_write_flag
Definition: logging.c:12
const char * name
Definition: options.h:51
void sm_log_pop()
Definition: logging.c:130
void sm_debug_write(int flag)
Definition: logging.c:16
int xterm_color_available
Definition: logging.c:27
void sm_write_context()
Definition: logging.c:135
void sm_debug(const char *msg,...)
Definition: logging.c:88
void sm_info(const char *msg,...)
Definition: logging.c:71
void sm_error(const char *msg,...)
Definition: logging.c:49
#define MAX_CONTEXTS
Definition: logging.c:114


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23