~ubuntu-branches/ubuntu/wily/ecasound/wily-proposed

« back to all changes in this revision

Viewing changes to libecasound/eca-logger.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-12 17:58:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110512175803-zy3lodjecabt9r3v
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// eca-logger.cpp: A logging subsystem implemented as a singleton class
 
3
// Copyright (C) 2002 Kai Vehmanen
 
4
//
 
5
// This program is free software; you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation; either version 2 of the License, or
 
8
// (at your option) any later version.
 
9
// 
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
// 
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program; if not, write to the Free Software
 
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
18
// ------------------------------------------------------------------------
 
19
 
 
20
#include <kvu_dbc.h>
 
21
#include <kvu_locks.h>
 
22
 
 
23
#include "eca-logger-interface.h"
 
24
#include "eca-logger-default.h"
 
25
#include "eca-logger.h"
 
26
 
 
27
ECA_LOGGER_INTERFACE* ECA_LOGGER::interface_impl_repp = 0;
 
28
pthread_mutex_t ECA_LOGGER::lock_rep = PTHREAD_MUTEX_INITIALIZER;
 
29
 
 
30
static const char *level_descs[] = {
 
31
  "ERROR   ", /* 0 */
 
32
  "INFO    ",
 
33
  "SUBSYST.",
 
34
  "MODULE  ",
 
35
  "OBJECTS ",
 
36
  "SYSTEM  ",
 
37
  "FUNCTION",
 
38
  "CONTINU.",
 
39
  "EIAM    ",
 
40
  "UNKNOWN "  /* 9 */
 
41
};
 
42
 
 
43
ECA_LOGGER_INTERFACE& ECA_LOGGER::instance(void)
 
44
{
 
45
  //
 
46
  // Note! Below we use the Double-Checked Locking Pattern
 
47
  //       to protect against concurrent access
 
48
 
 
49
  if (ECA_LOGGER::interface_impl_repp == 0) {
 
50
    KVU_GUARD_LOCK guard(&ECA_LOGGER::lock_rep);
 
51
    if (ECA_LOGGER::interface_impl_repp == 0) {
 
52
      ECA_LOGGER::interface_impl_repp = new ECA_LOGGER_DEFAULT();
 
53
    }
 
54
  }
 
55
  return(*interface_impl_repp);
 
56
}
 
57
 
 
58
void ECA_LOGGER::attach_logger(ECA_LOGGER_INTERFACE* logger)
 
59
{
 
60
  int oldloglevel = -1;
 
61
  if (interface_impl_repp != 0) {
 
62
    oldloglevel = interface_impl_repp->get_log_level_bitmask();
 
63
  }
 
64
  ECA_LOGGER::detach_logger();
 
65
  if (ECA_LOGGER::interface_impl_repp == 0) {
 
66
    KVU_GUARD_LOCK guard(&ECA_LOGGER::lock_rep);
 
67
    if (ECA_LOGGER::interface_impl_repp == 0) {
 
68
      ECA_LOGGER::interface_impl_repp = logger;
 
69
      if (oldloglevel != -1) {
 
70
        logger->set_log_level_bitmask(oldloglevel);
 
71
      }
 
72
    }
 
73
  }
 
74
  DBC_ENSURE(ECA_LOGGER::interface_impl_repp == logger);
 
75
}
 
76
 
 
77
/**
 
78
 * Detaches the current logger implementation.
 
79
 */
 
80
void ECA_LOGGER::detach_logger(void)
 
81
{
 
82
  if (ECA_LOGGER::interface_impl_repp != 0) {
 
83
    KVU_GUARD_LOCK guard(&ECA_LOGGER::lock_rep);
 
84
    if (ECA_LOGGER::interface_impl_repp != 0) {
 
85
      delete ECA_LOGGER::interface_impl_repp;
 
86
      ECA_LOGGER::interface_impl_repp = 0;
 
87
    }
 
88
  }
 
89
  DBC_ENSURE(ECA_LOGGER::interface_impl_repp == 0);
 
90
}
 
91
 
 
92
const char* ECA_LOGGER::level_to_string(ECA_LOGGER::Msg_level_t arg)
 
93
{
 
94
  switch(arg) 
 
95
  {
 
96
    case ECA_LOGGER::errors: return level_descs[0];
 
97
    case ECA_LOGGER::info: return level_descs[1];
 
98
    case ECA_LOGGER::subsystems: return level_descs[2];
 
99
    case ECA_LOGGER::module_names: return level_descs[3];
 
100
    case ECA_LOGGER::user_objects: return level_descs[4];
 
101
    case ECA_LOGGER::system_objects: return level_descs[5];
 
102
    case ECA_LOGGER::functions: return level_descs[6];
 
103
    case ECA_LOGGER::continuous: return level_descs[7];
 
104
    case ECA_LOGGER::eiam_return_values: return level_descs[8];
 
105
    default: return level_descs[9];
 
106
  }
 
107
}