Dlf.cpp 4.94 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
32
33
34
35
36
37
38
39
#include "castor/exception/Exception.hpp"
#include "castor/exception/Internal.hpp"

// -----------------------------------------------------------------------
// dlf_getPendingMessages
// -----------------------------------------------------------------------
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;
}
40
41

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

// -----------------------------------------------------------------------
// dlf_addMessages
// -----------------------------------------------------------------------
72
73
void castor::dlf::dlf_addMessages (int offset, Message messages[])
throw () {
74
  if (::dlf_isinitialized()) {
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    int i = 0;
    while (messages[i].number >= 0) {
      ::dlf_regtext(offset + messages[i].number,
		    messages[i].text.c_str());
      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
101
102
103
104
105
106
107
108
109
// -----------------------------------------------------------------------
// dlf_writep
// wrapper of the dlf writep that compounds the Cns_fileid struct
// -----------------------------------------------------------------------
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
111
112
113
114
115
116
  throw() {
  
  struct Cns_fileid ns_invariant;
  ns_invariant.fileid = fileId;
  strncpy(ns_invariant.server, nsHost.c_str(), sizeof(ns_invariant.server) - 1);
  
  castor::dlf::dlf_writep(uuid, severity, message_no, numparams, params, &ns_invariant);
117
}
118
119
120
121
122
123
124
125
126
127

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