1// vi:set ft=cpp: -*- Mode: C++ -*-
2/**
3 * \internal
4 * \file
5 * \brief Debug interface
6 */
7/*
8 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
9 *               Alexander Warg <warg@os.inf.tu-dresden.de>
10 *     economic rights: Technische Universität Dresden (Germany)
11 *
12 * This file is part of TUD:OS and distributed under the terms of the
13 * GNU General Public License 2.
14 * Please see the COPYING-GPL-2 file for details.
15 *
16 * As a special exception, you may use this file as part of a free software
17 * library without restriction.  Specifically, if other files instantiate
18 * templates or use macros or inline functions from this file, or you compile
19 * this file and link it with other files to produce an executable, this
20 * file does not by itself cause the resulting executable to be covered by
21 * the GNU General Public License.  This exception does not however
22 * invalidate any other reasons why the executable file might be covered by
23 * the GNU General Public License.
24 */
25#pragma once
26
27#include <l4/sys/types.h>
28
29namespace L4Re { namespace Util {
30class Err
31{
32public:
33  enum Level
34  {
35    Normal = 0,
36    Fatal,
37  };
38
39  static char const *const levels[];
40
41  void tag() const
42  { cprintf("%s: %s", _component, levels[_l]); }
43
44  int printf(char const *fmt, ...) const
45    __attribute__((format(printf,2,3)));
46
47  int cprintf(char const *fmt, ...) const
48    __attribute__((format(printf,2,3)));
49
50  Err(Level l, char const *component) : _l(l), _component(component)
51  {}
52
53private:
54  Level _l;
55  char const *_component;
56};
57
58
59class Dbg
60{
61private:
62  void tag() const;
63
64#ifndef NDEBUG
65
66  unsigned long _m;
67  char const *const _component;
68  char const *const _subsys;
69
70  int printf_impl(char const *fmt, ...) const
71    __attribute__((format(printf, 2, 3)));
72
73  int cprintf_impl(char const *fmt, ...) const
74    __attribute__((format(printf, 2, 3)));
75
76public:
77  static unsigned long level;
78
79  static void set_level(unsigned long l) { level = l; }
80
81  bool is_active() const { return _m & level; }
82
83  int __attribute__((always_inline, format(printf, 2, 3)))
84  printf(char const *fmt, ...) const
85  {
86    if (!(level & _m))
87      return 0;
88
89    return printf_impl(fmt, __builtin_va_arg_pack());
90  }
91
92  int __attribute__((always_inline, format(printf, 2, 3)))
93  cprintf(char const *fmt, ...) const
94  {
95    if (!(level & _m))
96      return 0;
97
98    return cprintf_impl(fmt, __builtin_va_arg_pack());
99  }
100
101  explicit
102  Dbg() : _m(1), _component(0), _subsys(0) { };
103
104  explicit
105  Dbg(unsigned long mask, char const *comp, char const *subs)
106  : _m(mask), _component(comp), _subsys(subs)
107  {}
108
109#else
110
111public:
112  static void set_level(unsigned long) {}
113  bool is_active() const { return false; }
114
115  int printf(char const * /*fmt*/, ...) const
116    __attribute__((format(printf, 2, 3)))
117  { return 0; }
118
119  int cprintf(char const * /*fmt*/, ...) const
120    __attribute__((format(printf, 2, 3)))
121  { return 0; }
122
123  explicit
124  Dbg() {}
125
126  explicit
127  Dbg(unsigned long, char const *, char const *) {}
128
129#endif
130
131};
132
133}}
134
135