Dlf.cpp 6.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/******************************************************************************
 *
 * 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
19
 * @(#)Dlf.cpp,v 1.1 $Release$ 2005/04/05 11:51:33 sponcec3
20
21
22
 *
 * C++ interface to DLF
 *
23
 * @author Castor Dev team, castor-dev@cern.ch
24
25
26
27
 *****************************************************************************/

// Include Files
#include "castor/dlf/Dlf.hpp"
28
#include "castor/exception/Exception.hpp"
29
#include "serrno.h"
30

31
32
33
#include <errno.h>

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

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

70
//-----------------------------------------------------------------------------
71
// dlf_addMessages
72
//-----------------------------------------------------------------------------
73
void castor::dlf::dlf_addMessages (int offset, Message messages[])
74
  throw() {
75
  if (::dlf_isinitialized()) {
76
77
78
    int i = 0;
    while (messages[i].number >= 0) {
      ::dlf_regtext(offset + messages[i].number,
79
                    messages[i].text.c_str());
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      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));
96
97
  }
}
98

99
//-----------------------------------------------------------------------------
100
101
// dlf_writep
// wrapper of the dlf writep that compounds the Cns_fileid struct
102
//-----------------------------------------------------------------------------
103
104
105
106
107
108
109
110
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[])
111
  throw() {
112

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

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

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

142
//-----------------------------------------------------------------------------
143
// dlf_writepc
144
//-----------------------------------------------------------------------------
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
162
  cparams[0].name             = (char*)"File";
163
164
  cparams[0].type             = DLF_MSG_PARAM_STR;
  cparams[0].value.par_string = (char *)file;
165
  cparams[1].name             = (char*)"Line";
166
167
  cparams[1].type             = DLF_MSG_PARAM_INT;
  cparams[1].value.par_int    = line;
168
  cparams[2].name             = (char*)"Function";
169
170
  cparams[2].type             = DLF_MSG_PARAM_STR;
  cparams[2].value.par_string = (char*)function;
171
172
173
174
175
176
177
178
179

  // 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;
}