XRootD
XrdSsiLogging.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S s i L o g g i n g . c c */
4 /* */
5 /* (c) 2016 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <fcntl.h>
31 #include <iostream>
32 #include <cstdio>
33 #include <cstdarg>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 
37 #include "XrdVersion.hh"
38 #include "XrdOuc/XrdOucEnv.hh"
39 #include "XrdOuc/XrdOucStream.hh"
40 #include "XrdSsi/XrdSsiLogger.hh"
41 #include "XrdSys/XrdSysE2T.hh"
42 #include "XrdSys/XrdSysLogPI.hh"
43 #include "XrdSys/XrdSysPlugin.hh"
44 
45 /******************************************************************************/
46 /* G l o b a l O b j e c t s */
47 /******************************************************************************/
48 
49 namespace XrdSsi
50 {
52 }
53 
54 using namespace XrdSsi;
55 
56 /******************************************************************************/
57 /* L o g P l u g i n H o o k s */
58 /******************************************************************************/
59 /******************************************************************************/
60 /* C o n f i g L o g */
61 /******************************************************************************/
62 
63 namespace
64 {
65 void ConfigLog(const char *cFN)
66 {
67  XrdVERSIONINFODEF(myVersion, ssi, XrdVNUMBER, XrdVERSION);
68  const char *lName;
69  char eBuff[2048], *var, *val, **lDest, *logPath = 0, *svcPath = 0;
70  XrdSysPlugin *myLib;
71  XrdSsiLogger::MCB_t **theCB;
72  XrdOucEnv myEnv;
73  XrdOucStream cStrm(0, getenv("XRDINSTANCE"), &myEnv, "=====> ");
74  int cfgFD, retc, NoGo = 0;
75 
76 // Try to open the configuration file.
77 //
78  if ((cfgFD = open(cFN, O_RDONLY, 0)) < 0)
79  {std::cerr <<"Config " <<XrdSysE2T(errno) <<" opening " <<cFN <<std::endl;
80  return;
81  }
82  cStrm.Attach(cfgFD);
83  static const char *cvec[] = { "*** ssi logging plugin config:", 0 };
84  cStrm.Capture(cvec);
85 
86 // Now start reading records until eof.
87 //
88  while((var = cStrm.GetMyFirstWord()))
89  { if (!strcmp(var, "ssi.loglib")) {lDest = &logPath; lName = "log";}
90  else if (!strcmp(var, "ssi.svclib")) {lDest = &svcPath; lName = "svc";}
91  else continue;
92  if (!(val = cStrm.GetWord()) || !val[0])
93  {std::cerr <<"Config "<<lName<<"lib path not specified."<<std::endl; NoGo=1;}
94  else {if (*lDest) free(*lDest);
95  *lDest = strdup(val);
96  }
97  }
98 
99 // Now check if any errors occurred during file i/o
100 //
101  if ((retc = cStrm.LastError()))
102  {std::cerr <<"Config " <<XrdSysE2T(-retc) <<" reading " <<cFN <<std::endl;
103  NoGo = 1;
104  }
105  cStrm.Close();
106 
107 // If we don't have a loglib then revert to using svclib
108 //
109  if (!logPath) {logPath = svcPath; svcPath = 0; lName = "svclib";}
110  else lName = "loglib";
111 
112 // Check if we have a logPath (we must)
113 //
114  if (!NoGo && !logPath)
115  {std::cerr <<"Config neither ssi.loglib nor ssi.svclib directive specified in "
116  <<cFN <<std::endl;
117  return;
118  }
119 
120 // Create a plugin object
121 //
122  if (!(myLib = new XrdSysPlugin(eBuff, sizeof(eBuff), logPath, lName,
123  &myVersion)))
124  {std::cerr <<"Config " <<eBuff <<std::endl;
125  return;
126  }
127 
128 // Now get the entry point of the message callback function if the dynamic
129 // initialization of the plugin library hasn't already set it.
130 //
131  if (!msgCB)
132  {theCB = (XrdSsiLogger::MCB_t **)(myLib->getPlugin("XrdSsiLoggerMCB"));
133  if (!msgCB && !theCB) std::cerr <<"Config " <<eBuff <<std::endl;
134  else {if (!msgCB) msgCB = *theCB;
135  myLib->Persist();
136  }
137  }
138  else myLib->Persist();
139 
140 // All done
141 //
142  delete myLib;
143 }
144 }
145 
146 /******************************************************************************/
147 /* X r d S y s L o g P I n i t */
148 /******************************************************************************/
149 
150 extern "C"
151 {
152 XrdSysLogPI_t XrdSysLogPInit(const char *cfgfn, char **argv, int argc)
153  {if (cfgfn && *cfgfn) ConfigLog(cfgfn);
154  if (!msgCB)
155  std::cerr <<"Config '-l@' requires a logmsg callback function "
156  <<"but it was found!" <<std::endl;
157  return msgCB;
158  }
159 }
160 
int open(const char *path, int oflag,...)
XrdVERSIONINFO(XrdSysLogPInit, XrdSsiLPI)
XrdSysLogPI_t XrdSysLogPInit(const char *cfgfn, char **argv, int argc)
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104
void(* XrdSysLogPI_t)(struct timeval const &mtime, unsigned long tID, const char *msg, int mlen)
Definition: XrdSysLogPI.hh:51
void() MCB_t(struct timeval const &mtime, unsigned long tID, const char *msg, int mlen)
Length of message text.
void * getPlugin(const char *pname, int optional=0)
void * Persist()
XrdVersionInfo myVersion
XrdVERSIONINFODEF(myVersion, cmsclient, XrdVNUMBER, XrdVERSION)
XrdSsiLogger::MCB_t * msgCB
Definition: XrdSsiLogger.cc:59