Dlf.cpp 6.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *                      dlf_write.cpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
Vitali Motyakov's avatar
Vitali Motyakov committed
20
 * @(#)Dlf.cpp,v 1.1 $Release$ 2005/04/05 11:51:33 sponcec3
21
22
23
24
25
26
27
28
 *
 * C++ interface to DLF
 *
 * @author Sebastien Ponce
 *****************************************************************************/

// Include Files
#include "castor/dlf/Dlf.hpp"
29
30
31
#include "castor/exception/Exception.hpp"
#include "castor/exception/Internal.hpp"

32
33
34
35
#include <errno.h>


//-----------------------------------------------------------------------------
36
// dlf_getPendingMessages
37
//-----------------------------------------------------------------------------
38
39
40
41
42
std::vector<std::pair<int, castor::dlf::Message*> >&
castor::dlf::dlf_getPendingMessages () throw() {
  static std::vector<std::pair<int, castor::dlf::Message*> > pendingMessages;
  return pendingMessages;
}
43

44
//-----------------------------------------------------------------------------
45
// dlf_init
46
//-----------------------------------------------------------------------------
47
void castor::dlf::dlf_init
48
(const char* facilityName, castor::dlf::Message messages[])
49
  throw (castor::exception::Exception) {
50
51
  // Initialise the DLF interface
  if (::dlf_init(facilityName, -1) != 0) {
52
    castor::exception::Internal ex;
53
    ex.getMessage() << "Unable to initialize DLF: " << sstrerror(errno);
54
55
    throw ex;
  }
56
  // Register the facility's messages with the interface. We do this even
57
  // if the interface fails to initialisation as it is used for local
58
  // logging
59
  dlf_addMessages(0, messages);
60
61
  // Also register the pending messages
  for (std::vector<std::pair<int, Message*> >::const_iterator it =
62
         dlf_getPendingMessages().begin();
63
64
65
66
67
68
       it != dlf_getPendingMessages().end();
       it++) {
    dlf_addMessages(it->first, it->second);
    delete[](it->second);
  }
  dlf_getPendingMessages().clear();
69
70
}

71
//-----------------------------------------------------------------------------
72
// dlf_addMessages
73
//-----------------------------------------------------------------------------
74
void castor::dlf::dlf_addMessages (int offset, Message messages[])
75
  throw () {
76
  if (::dlf_isinitialized()) {
77
78
79
    int i = 0;
    while (messages[i].number >= 0) {
      ::dlf_regtext(offset + messages[i].number,
80
                    messages[i].text.c_str());
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      i++;
    }
  } else {
    // replicate message array
    int len = 0;
    while (messages[len].number >= 0) {
      len++;
    }
    Message* lmessages = new Message[len+1];
    lmessages[len].number = -1;
    for (int i = 0; i < len; i++) {
      lmessages[i].number = messages[i].number;
      lmessages[i].text = messages[i].text;
    }
    // and store it for further usage when DLF will be initialized
    dlf_getPendingMessages().push_back(std::pair<int, Message*>(offset, lmessages));
97
98
  }
}
99

100
//-----------------------------------------------------------------------------
101
102
// dlf_writep
// wrapper of the dlf writep that compounds the Cns_fileid struct
103
//-----------------------------------------------------------------------------
104
105
106
107
108
109
110
111
void castor::dlf::dlf_writep
(Cuuid_t uuid,
 int severity,
 int message_no,
 u_signed64 fileId,
 std::string nsHost,
 int numparams,
 castor::dlf::Param params[])
112
  throw() {
113

114
115
116
  struct Cns_fileid ns_invariant;
  ns_invariant.fileid = fileId;
  strncpy(ns_invariant.server, nsHost.c_str(), sizeof(ns_invariant.server) - 1);
117

118
  castor::dlf::dlf_writep(uuid, severity, message_no, numparams, params, &ns_invariant);
119
}
120

121
//-----------------------------------------------------------------------------
122
// dlf_writep
123
//-----------------------------------------------------------------------------
124
125
126
127
128
129
void castor::dlf::dlf_writep
(Cuuid_t uuid,
 int severity,
 int message_no,
 int numparams,
 castor::dlf::Param params[],
130
 struct Cns_fileid *ns_invariant) throw() {
131
  // Place holder for the C version of the parameters
132
  // dlf_write_param_t cparams[numparams]; // Doesn't work on windows compiler!!!
Vitali Motyakov's avatar
Vitali Motyakov committed
133
  dlf_write_param_t* cparams = new dlf_write_param_t[numparams];
134
  // Translate parameters from C++ to C
135
136
137
138
139
  for (int i = 0; i < numparams; i++) {
    cparams[i] = params[i].cParam();
  }
  ::dlf_writep(uuid, severity, message_no,
               ns_invariant, numparams, cparams);
Vitali Motyakov's avatar
Vitali Motyakov committed
140
  delete[] cparams;
141
}
142

143
//-----------------------------------------------------------------------------
144
// dlf_writepc
145
//-----------------------------------------------------------------------------
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
void castor::dlf::dlf_writepc
(const char *file,
 const int line,
 const char *function,
 Cuuid_t uuid,
 int severity,
 int message_no,
 int numparams,
 castor::dlf::Param params[],
 struct Cns_fileid *ns_invariant) throw() {
  // Place holder for the C version of the parameters, allocate 3 more
  // parameters for the context parameters: file, line and function
  const int numcontextparams = 3;
  const int numcparams = numparams + numcontextparams;
  dlf_write_param_t* cparams = new dlf_write_param_t[numcparams];

  // Fill the context parameters: file, line and function
163
164
165
166
167
168
169
170
171
  cparams[0].name             = "File";
  cparams[0].type             = DLF_MSG_PARAM_STR;
  cparams[0].value.par_string = (char *)file;
  cparams[1].name             = "Line";
  cparams[1].type             = DLF_MSG_PARAM_INT;
  cparams[1].value.par_int    = line;
  cparams[2].name             = "Function";
  cparams[2].type             = DLF_MSG_PARAM_STR;
  cparams[2].value.par_string = (char*)function;
172
173
174
175
176
177
178
179
180

  // Translate parameters from C++ to C
  for (int i = 0; i < numparams; i++) {
    cparams[i+numcontextparams] = params[i].cParam();
  }
  ::dlf_writep(uuid, severity, message_no,
               ns_invariant, numcparams, cparams);
  delete[] cparams;
}