]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/debug.c
new version 0.2.3
[orte.git] / orte / liborte / debug.c
1 /*
2  *  $Id: debug.c,v 0.0.0.1              2003/08/21
3  *
4  *  DEBUG:  section 1                   Debug
5  *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
6  *
7  *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
8  *  --------------------------------------------------------------------
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  */
21
22 #include "orte_all.h"
23
24 /* global debug variables */
25 int db_level;
26 int debugLevels[MAX_DEBUG_SECTIONS];
27 int mem_check_counter=0;
28 NtpTime zNtpTime,iNtpTime;
29 SequenceNumber noneSN;
30
31 /*********************************************************************/
32 /* forvard declarations */
33 static void
34 db_print_output(const char *format);
35
36 /*********************************************************************/
37 /* globals */
38
39 #ifndef CONFIG_ORTE_RT
40 FILE *debug_log=NULL;       /* NULL */
41 #endif
42
43 /*********************************************************************/
44 /* functions */
45
46 #ifdef CONFIG_ORTE_RT
47 static const char *
48 debug_log_time(void) {
49   struct timespec        time;
50   static char            buf[64];
51
52   clock_gettime(CLOCK_REALTIME, &time);
53   sprintf(buf, "%li.%03li", time.tv_sec,time.tv_nsec/1000000);
54   return buf;
55 }
56 #else
57 static const char *
58 debug_log_time(void) {
59   struct timeval        time;
60   static char         buf[64];
61
62   gettimeofday(&time,NULL);
63   sprintf(buf, "%li.%03li", time.tv_sec,time.tv_usec/1000);
64   return buf;
65 }
66 #endif /* CONFIG_ORTE_RT */
67
68 void
69 db_print(const char *format,...) {
70   char f[256];
71   va_list ap;
72
73   va_start(ap, format);
74   sprintf(f, "%s | ",debug_log_time());
75   vsprintf(f+strlen(f),format,ap);
76   va_end(ap);
77   db_print_output(f);    
78 }
79
80 void
81 db_print_output(const char *format) {
82 #ifndef CONFIG_ORTE_RT
83   if (debug_log == NULL) return;
84   fprintf(debug_log, format);
85   fflush(debug_log);
86 #else
87   rtl_printf(format);
88 #endif
89 }
90
91 void
92 debug_arg(const char *arg) {
93   int32_t s=0,l=0,i;
94
95   if (!strncmp(arg, "ALL", 3)) {
96     s = -1;
97     arg += 4;
98   } else {
99     s = atoi(arg);
100     while (*arg && *arg++ != '.');
101   }
102   l = atoi(arg);
103   if (l < 0) l = 0;
104   if (l > 10) l = 10;
105   if (s >= 0) {
106     debugLevels[s] = l;
107     return;
108   }
109   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
110     debugLevels[i] = l;
111
112
113 void 
114 debug_options(const char *options) {
115   char *p = NULL;
116   char *s = NULL;
117
118   if (options) {
119     p = strdup((char *)options);
120     for (s = strtok(p, ":"); s; s = strtok(NULL, ":"))
121       debug_arg(s);
122     free(p);
123   }     
124 }
125
126 void
127 debug_open_log(const char *logfile) {
128 #ifndef CONFIG_ORTE_RT
129   if (logfile == NULL) {
130     debug_log = stderr;
131     return;
132   }
133   if (debug_log && debug_log != stderr)
134     fclose(debug_log);
135   debug_log = fopen(logfile, "a+");
136   if (!debug_log) {
137     fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile);
138     perror(logfile);
139     fprintf(stderr, "         messages will be sent to 'stderr'.\n");
140     fflush(stderr);
141     debug_log = stderr;
142   }
143 #endif
144 }
145
146 void
147 db_init(const char *logfile, const char *options) {
148   int i;
149
150   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
151     debugLevels[i] = -1;
152   debug_options(options);
153   debug_open_log(logfile);
154 }
155
156 #ifdef ENABLE_MEM_CHECK
157 void *mem_check_malloc(size_t size) { 
158   void *ptr;
159   
160   if ((ptr=malloc(size))) {
161     mem_check_counter++;
162     debug(1,9) ("mem check: inc %d\n",mem_check_counter);
163   }
164   return ptr;
165 }
166
167 void mem_check_free(void *ptr) {
168   if(!ptr) {
169 //    LOG_FATAL(KERN_CRIT "ul_mem_check_free : triing to free NULL ptr\n");
170   }else{
171     mem_check_counter--;
172     debug(1,9) ("mem check: dec %d\n",mem_check_counter);
173     free(ptr);
174   }
175 }
176 #endif /* ENABLE_MEM_CHECK */