Dlf.hpp 9.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/******************************************************************************
 *
 * 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.
 *
 *
 * C++ interface to DLF
 *
22
 * @author Castor Dev team, castor-dev@cern.ch
23
24
 *****************************************************************************/

25
#pragma once
26
27
28

#include "castor/dlf/Message.hpp"
#include "castor/dlf/Param.hpp"
29
#include "castor/exception/Exception.hpp"
Steven Murray's avatar
Steven Murray committed
30

31
#include <shift/dlf_api.h>
32
#include <vector>
33

34
/**
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 * Logs a message and adds the context information of file, line and function
 * as three parameters of the message.
 */
#define CASTOR_DLF_WRITEC(uuid, severity, message_no) \
  castor::dlf::dlf_writepc( \
    __FILE__, \
    __LINE__, \
    __PRETTY_FUNCTION__, \
    uuid, \
    severity, \
    message_no)

/**
 * Logs a message with a set of parameters and automatically generates and
 * adds the context information of file, line and function to the parameters.
50
51
 */
#define CASTOR_DLF_WRITEPC(uuid, severity, message_no, params) \
52
  castor::dlf::dlf_writepc( \
53
54
55
56
57
58
59
60
    __FILE__, \
    __LINE__, \
    __PRETTY_FUNCTION__, \
    uuid, \
    severity, \
    message_no, \
    params)

61
62
63
64
namespace castor {

  namespace dlf {

65
66
67
68
69
70
71
72
    /**
     * Vector of messages that should be declared
     * at initialization. These messages were sent to
     * dlf_addMessage before it was initialized and
     * thus have to wait here.
     */
    std::vector<std::pair<int, castor::dlf::Message*> >&
    dlf_getPendingMessages() throw();
73
74
75
76
77
78
79
 
    /**
     * Helper class adding a cleanup for unclaimed pending
     * messages. This is for the case when dlf_init never gets
     * called (new done in castor::dlf::dlf_addMessages for 
     * static data from castorclient (?) library.
     */
80
    class PendingMessagesVector: 
81
82
    public std::vector<std::pair<int, castor::dlf::Message*> > {
    public:
83
      ~PendingMessagesVector() {
84
85
86
87
88
89
90
91
92
        for (std::vector<std::pair<int, Message*> >::iterator it =
               dlf_getPendingMessages().begin();
             it != dlf_getPendingMessages().end();
             it++) {
          delete[](it->second);
          it->second = NULL;
        }
      }
    };
93

94
95
96
    /**
     * Initialization of the DLF logging system
     * @param facilityName name of the DLF facility to use
97
     * @param messages array of messages to declare in the
98
99
     * facility. The end of the array is marked by a
     * message with negative number.
100
     * @param throws a CASTOR exception in case of failure
101
     */
102
    void dlf_init(const char* facilityName,
103
                  Message messages[])
104
      ;
105

106
    /**
107
108
109
110
111
112
113
     * Adds messages to the current DLF facility.
     * This method can be called before the initialization of DLF
     * if needed. In such a case, the messages will be stored
     * and added at initialization time.
     * Note that no exception will ever be thrown in case of failure.
     * Failures will actually be silently ignored in order to not
     * impact the processing.
114
115
116
117
118
119
120
     * @param offset the offset to add to each message number.
     * This is to avoid collisions with previously added messages
     * @param messages array of messages to decalre in the
     * facility. The end of the array is marked by a
     * message with negative number.
     */
    void dlf_addMessages(int offset,
121
                         Message messages[]) throw();
122

123
    /**
124
125
126
     * prints a message into dlf. Note that no exception will ever
     * be thrown in case of failure. Failures will actually be silently
     * ignored in order to not impact the processing.
127
128
129
130
131
132
133
134
135
136
137
138
139
     * @param uuid the uuid of the component issuing the message
     * @param message_no the message number in the facility.
     * @param severity the severity of the message.
     * @param numparams the number of parameters in the message
     * @param params the parameters of the message, given as an array
     * @ns_invariant the castor file concerned by the message
     * (if any), given as a name server fileId.
     */
    void dlf_writep (Cuuid_t uuid,
                     int severity,
                     int message_no,
                     int numparams = 0,
                     castor::dlf::Param params[] = 0,
140
                     struct Cns_fileid *ns_invariant = 0) throw();
141
142

    /**
143
144
145
146
     * A template function that wraps dlf_writep in order to get the compiler
     * to automatically determine the size of the params parameter, therefore
     * removing the need for the devloper to provide it explicity.
     */
147
    template<int numparams>
148
149
150
    void dlf_writep (Cuuid_t uuid,
                     int severity,
                     int message_no,
151
                     castor::dlf::Param (&params)[numparams],
152
                     struct Cns_fileid *ns_invariant = 0) throw() {
153
      dlf_writep(uuid, severity, message_no, numparams, params, ns_invariant);
154
155
156
157
158
    }

    /**
     * prints a message together with the context information file, line and
     * function into dlf. Note that no exception will ever
159
160
     * be thrown in case of failure. Failures will actually be silently
     * ignored in order to not impact the processing.
161
162
163
     * @param file the name of the file where dlf_writepc was called.
     * @param line the number of the line where dlf_writepc was called.
     * @param function the name of the function where dlf_writepc was called.
164
165
166
167
168
169
170
171
     * @param uuid the uuid of the component issuing the message
     * @param message_no the message number in the facility.
     * @param severity the severity of the message.
     * @param numparams the number of parameters in the message
     * @param params the parameters of the message, given as an array
     * @ns_invariant the castor file concerned by the message
     * (if any), given as a name server fileId.
     */
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    void dlf_writepc (const char *file,
                      const int line,
                      const char *function,
                      Cuuid_t uuid,
                      int severity,
                      int message_no,
                      int numparams = 0,
                      castor::dlf::Param params[] = 0,
                      struct Cns_fileid *ns_invariant = 0) throw();

    /**
     * A template function that wraps dlf_writepc in order to get the compiler
     * to automatically determine the size of the params parameter, therefore
     * removing the need for the devloper to provide it explicity.
     */
187
    template<int numparams>
188
189
190
191
192
193
    void dlf_writepc (const char *file,
                      const int line,
                      const char *function,
                      Cuuid_t uuid,
                      int severity,
                      int message_no,
194
                      castor::dlf::Param (&params)[numparams],
195
                      struct Cns_fileid *ns_invariant = 0) throw() {
196
197
      dlf_writepc(file, line, function, uuid, severity, message_no, numparams,
        params, ns_invariant);
198
    }
199

200
    /**
201
202
203
204
205
206
207
208
209
210
211
212
213
     * wrapper to dlf_writte but it compounds the struct Cns_fileId
     * ns_invariantfrom the prints a message into dlf. Note that no
     * exception will ever
     * be thrown in case of failure. Failures will actually be silently
     * ignored in order to not impact the processing.
     * @param uuid the uuid of the component issuing the message
     * @param message_no the message number in the facility.
     * @param severity the severity of the message.
     * @param numparams the number of parameters in the message
     * @param params the parameters of the message, given as an array
     * @param fileId the castor file id
     * @param nsHost the name server
     */
214
    void dlf_writep (Cuuid_t uuid,
215
216
                     int severity,
                     int message_no,
217
                     u_signed64 fileId,
218
219
220
221
                     std::string nsHost,
                     int numparams = 0,
                     castor::dlf::Param params[] = 0) throw();

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    /**
     * A template function that wraps dlf_writep in order to get the compiler
     * to automatically determine the size of the params parameter, therefore
     * removing the need for the devloper to provide it explicity.
     */
    template<int numparams>
    void dlf_writep (Cuuid_t uuid,
                     int severity,
                     int message_no,
                     u_signed64 fileId,
                     std::string nsHost,
                     castor::dlf::Param (&params)[numparams]) throw() {
      dlf_writep(uuid, severity, message_no, fileId, nsHost, numparams,
        params);
    }

238
  } // end of namespace dlf
239
240
241
242

} // end of namespace castor