aboutsummaryrefslogtreecommitdiff
path: root/tools/debugserver/source/DNBLog.cpp
blob: 18d8d2ad3a674eb4cd240ec8c6945525396fd41f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//===-- DNBLog.cpp ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  Created by Greg Clayton on 6/18/07.
//
//===----------------------------------------------------------------------===//

#include "DNBLog.h"

static int g_debug = 0;
static int g_verbose = 0;

#if defined (DNBLOG_ENABLED)

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <mach/mach.h>
#include <pthread.h>
#include "PThreadMutex.h"

uint32_t g_log_bits = 0;
static DNBCallbackLog g_log_callback = NULL;
static void *g_log_baton = NULL;


int
DNBLogGetDebug ()
{
    return g_debug;
}


void
DNBLogSetDebug (int g)
{
    g_debug = g;
}

int
DNBLogGetVerbose ()
{
    return g_verbose;
}

void
DNBLogSetVerbose (int v)
{
    g_verbose = v;
}

bool
DNBLogCheckLogBit (uint32_t bit)
{
    return (g_log_bits & bit) != 0;
}

uint32_t
DNBLogSetLogMask (uint32_t mask)
{
    uint32_t old = g_log_bits;
    g_log_bits = mask;
    return old;
}

uint32_t
DNBLogGetLogMask ()
{
    return g_log_bits;
}

void
DNBLogSetLogCallback (DNBCallbackLog callback, void *baton)
{
    g_log_callback = callback;
    g_log_baton = baton;
}

DNBCallbackLog
DNBLogGetLogCallback ()
{
    return g_log_callback;
}

bool
DNBLogEnabled ()
{
    return g_log_callback != NULL;
}

bool
DNBLogEnabledForAny (uint32_t mask)
{
    if (g_log_callback)
        return (g_log_bits & mask) != 0;
    return false;
}
static inline void
_DNBLogVAPrintf(uint32_t flags, const char *format, va_list args)
{
    static PThreadMutex g_LogThreadedMutex(PTHREAD_MUTEX_RECURSIVE);
    PTHREAD_MUTEX_LOCKER(locker, g_LogThreadedMutex);

    if (g_log_callback)
      g_log_callback(g_log_baton, flags, format, args);
}

void
_DNBLog(uint32_t flags, const char *format, ...)
{
    va_list args;
    va_start (args, format);
    _DNBLogVAPrintf(flags, format, args);
    va_end (args);
}

//----------------------------------------------------------------------
// Print debug strings if and only if the global g_debug is set to
// a non-zero value.
//----------------------------------------------------------------------
void
_DNBLogDebug (const char *format, ...)
{
    if (DNBLogEnabled () && g_debug)
    {
        va_list args;
        va_start (args, format);
        _DNBLogVAPrintf(DNBLOG_FLAG_DEBUG, format, args);
        va_end (args);
    }
}


//----------------------------------------------------------------------
// Print debug strings if and only if the global g_debug is set to
// a non-zero value.
//----------------------------------------------------------------------
void
_DNBLogDebugVerbose (const char *format, ...)
{
    if (DNBLogEnabled () && g_debug && g_verbose)
    {
        va_list args;
        va_start (args, format);
        _DNBLogVAPrintf(DNBLOG_FLAG_DEBUG | DNBLOG_FLAG_VERBOSE, format, args);
        va_end (args);
    }
}


static uint32_t g_message_id = 0;

//----------------------------------------------------------------------
// Prefix the formatted log string with process and thread IDs and
// suffix it with a newline.
//----------------------------------------------------------------------
void
_DNBLogThreaded (const char *format, ...)
{
    if (DNBLogEnabled ())
    {
        //PTHREAD_MUTEX_LOCKER(locker, GetLogThreadedMutex());

        char *arg_msg = NULL;
        va_list args;
        va_start (args, format);
        ::vasprintf (&arg_msg, format, args);
        va_end (args);

        if (arg_msg != NULL)
        {
            static struct timeval g_timeval = { 0 , 0 };
            static struct timeval tv;
            static struct timeval delta;
            gettimeofday(&tv, NULL);
            if (g_timeval.tv_sec == 0)
            {
                delta.tv_sec = 0;
                delta.tv_usec = 0;
            }
            else
            {
                timersub (&tv, &g_timeval, &delta);
            }
            g_timeval = tv;
            
            // Calling "mach_port_deallocate()" bumps the reference count on the thread
            // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
            // count.
            thread_port_t thread_self = mach_thread_self();

            _DNBLog (DNBLOG_FLAG_THREADED, "%u +%lu.%06u sec [%4.4x/%4.4x]: %s", 
                     ++g_message_id, 
                     delta.tv_sec, 
                     delta.tv_usec,             
                     getpid(), 
                     thread_self, 
                     arg_msg);

            mach_port_deallocate(mach_task_self(), thread_self);
            free (arg_msg);
        }
    }
}

//----------------------------------------------------------------------
// Prefix the formatted log string with process and thread IDs and
// suffix it with a newline.
//----------------------------------------------------------------------
void
_DNBLogThreadedIf (uint32_t log_bit, const char *format, ...)
{
    if (DNBLogEnabled () && (log_bit & g_log_bits) == log_bit)
    {
        //PTHREAD_MUTEX_LOCKER(locker, GetLogThreadedMutex());

        char *arg_msg = NULL;
        va_list args;
        va_start (args, format);
        ::vasprintf (&arg_msg, format, args);
        va_end (args);

        if (arg_msg != NULL)
        {
            static struct timeval g_timeval = { 0 , 0 };
            static struct timeval tv;
            static struct timeval delta;
            gettimeofday(&tv, NULL);
            if (g_timeval.tv_sec == 0)
            {
                delta.tv_sec = 0;
                delta.tv_usec = 0;
            }
            else
            {
                timersub (&tv, &g_timeval, &delta);
            }
            g_timeval = tv;

            // Calling "mach_port_deallocate()" bumps the reference count on the thread
            // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
            // count.
            thread_port_t thread_self = mach_thread_self();

            _DNBLog (DNBLOG_FLAG_THREADED, "%u +%lu.%06u sec [%4.4x/%4.4x]: %s",
                     ++g_message_id, 
                     delta.tv_sec, 
                     delta.tv_usec, 
                     getpid(), 
                     thread_self, 
                     arg_msg);

            mach_port_deallocate(mach_task_self(), thread_self);

            free (arg_msg);
        }
    }
}



//----------------------------------------------------------------------
// Printing of errors that are not fatal.
//----------------------------------------------------------------------
void
_DNBLogError (const char *format, ...)
{
    if (DNBLogEnabled ())
    {
        char *arg_msg = NULL;
        va_list args;
        va_start (args, format);
        ::vasprintf (&arg_msg, format, args);
        va_end (args);

        if (arg_msg != NULL)
        {
            _DNBLog (DNBLOG_FLAG_ERROR, "error: %s", arg_msg);
            free (arg_msg);
        }
    }
}

//----------------------------------------------------------------------
// Printing of errors that ARE fatal. Exit with ERR exit code
// immediately.
//----------------------------------------------------------------------
void
_DNBLogFatalError (int err, const char *format, ...)
{
    if (DNBLogEnabled ())
    {
        char *arg_msg = NULL;
        va_list args;
        va_start (args, format);
        ::vasprintf (&arg_msg, format, args);
        va_end (args);

        if (arg_msg != NULL)
        {
            _DNBLog (DNBLOG_FLAG_ERROR | DNBLOG_FLAG_FATAL, "error: %s", arg_msg);
            free (arg_msg);
        }
        ::exit (err);
    }
}


//----------------------------------------------------------------------
// Printing of warnings that are not fatal only if verbose mode is
// enabled.
//----------------------------------------------------------------------
void
_DNBLogVerbose (const char *format, ...)
{
    if (DNBLogEnabled () && g_verbose)
    {
        va_list args;
        va_start (args, format);
        _DNBLogVAPrintf(DNBLOG_FLAG_VERBOSE, format, args);
        va_end (args);
    }
}

//----------------------------------------------------------------------
// Printing of warnings that are not fatal only if verbose mode is
// enabled.
//----------------------------------------------------------------------
void
_DNBLogWarningVerbose (const char *format, ...)
{
    if (DNBLogEnabled () && g_verbose)
    {
        char *arg_msg = NULL;
        va_list args;
        va_start (args, format);
        ::vasprintf (&arg_msg, format, args);
        va_end (args);

        if (arg_msg != NULL)
        {
            _DNBLog (DNBLOG_FLAG_WARNING | DNBLOG_FLAG_VERBOSE, "warning: %s", arg_msg);
            free (arg_msg);
        }
    }
}
//----------------------------------------------------------------------
// Printing of warnings that are not fatal.
//----------------------------------------------------------------------
void
_DNBLogWarning (const char *format, ...)
{
    if (DNBLogEnabled ())
    {
        char *arg_msg = NULL;
        va_list args;
        va_start (args, format);
        ::vasprintf (&arg_msg, format, args);
        va_end (args);

        if (arg_msg != NULL)
        {
            _DNBLog (DNBLOG_FLAG_WARNING, "warning: %s", arg_msg);
            free (arg_msg);
        }
    }
}

#endif