]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/debug.c
patch for rtnet
[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=(char*)MALLOC(strlen(options)+1);
120     if (p) {
121       memcpy(p, options, strlen(options) + 1);
122     }    
123     for (s = strtok(p, ":"); s; s = strtok(NULL, ":"))
124       debug_arg(s);
125     FREE(p);
126   }     
127 }
128
129 void
130 debug_open_log(const char *logfile) {
131 #ifndef CONFIG_ORTE_RT
132   if (logfile == NULL) {
133     debug_log = stderr;
134     return;
135   }
136   if (debug_log && debug_log != stderr)
137     fclose(debug_log);
138   debug_log = fopen(logfile, "a+");
139   if (!debug_log) {
140     fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile);
141     perror(logfile);
142     fprintf(stderr, "         messages will be sent to 'stderr'.\n");
143     fflush(stderr);
144     debug_log = stderr;
145   }
146 #endif
147 }
148
149 void
150 db_init(const char *logfile, const char *options) {
151   int i;
152
153   for (i = 0; i < MAX_DEBUG_SECTIONS; i++)
154     debugLevels[i] = -1;
155   debug_options(options);
156   debug_open_log(logfile);
157 }
158
159 #ifdef ENABLE_MEM_CHECK
160 void *mem_check_malloc(size_t size) { 
161   void *ptr;
162   
163   if ((ptr=malloc(size))) {
164     mem_check_counter++;
165     debug(1,9) ("mem check: inc %d\n",mem_check_counter);
166   }
167   return ptr;
168 }
169
170 void mem_check_free(void *ptr) {
171   if(!ptr) {
172 //    LOG_FATAL(KERN_CRIT "ul_mem_check_free : triing to free NULL ptr\n");
173   }else{
174     mem_check_counter--;
175     debug(1,9) ("mem check: dec %d\n",mem_check_counter);
176     free(ptr);
177   }
178 }
179 #endif /* ENABLE_MEM_CHECK */