Dlf.cpp 6.26 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
29
#include "castor/exception/Exception.hpp"

30
31
32
#include <errno.h>

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

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

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

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

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

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

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

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

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