StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TxLogEventCmd.C
1 #include "TxEventLog.h"
2 #include "TxEventLogFactory.h"
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <getopt.h>
7 #include <string>
8 #include <iostream>
9 
10 void printUsage () {
11  std::cout << "\nUSAGE:"
12  << "\n TxLogEvent"
13  << "\n -r/--requester <requester name> : required"
14  << "\n -j/--brokerjobid <broker job id> : required"
15  << "\n -t/--brokertaskid <broker task id> : required"
16  << "\n -k/--key <message key> : required"
17  << "\n -v/--value <message value> : required"
18  << "\n -l/--level <event level> : optional"
19  << "\n -c/--context <event context> : optional"
20  << std::endl << std::endl;
21 }
22 
23 int getOpts (int argc, char** argv, std::string& requester,
24  std::string& brokerJobID, std::string& brokerTaskID,
25  std::string& key, std::string& value,
26  std::string& context, std::string& level)
27 {
28  static const char *optString = "r:j:t:c:l:s:k:v:h?";
29  static struct option longOpts [] = {
30  {"requester", required_argument, NULL, 'r'},
31  {"brokerjobid", required_argument, NULL, 'j'},
32  {"brokertaskid", required_argument, NULL, 't'},
33  {"key", required_argument, NULL, 'k'},
34  {"value", required_argument, NULL, 'v'},
35  {"context", required_argument, NULL, 'c'},
36  {"level", required_argument, NULL, 'l'},
37  {"help", no_argument, NULL, 'h'}
38  };
39  int longIndex = 0;
40 
41  int opt = getopt_long (argc, argv, optString, longOpts, &longIndex);
42  if (opt == -1) {
43  printUsage ();
44  return 1;
45  }
46 
47  bool rOpt = false, jOpt = false, tOpt = false, kOpt = false, vOpt = false;
48  while (opt != -1) {
49  switch (opt) {
50  case 'r':
51  rOpt = true;
52  requester = optarg;
53  break;
54 
55  case 'j':
56  jOpt = true;
57  brokerJobID = optarg;
58  break;
59 
60  case 't':
61  tOpt = true;
62  brokerTaskID = optarg;
63  break;
64 
65  case 'k':
66  kOpt = true;
67  key = optarg;
68  break;
69 
70  case 'v':
71  vOpt = true;
72  value = optarg;
73  break;
74 
75  case 'c':
76  context = optarg;
77  break;
78 
79  case 'l':
80  level = optarg;
81  break;
82 
83  case 'h':
84  case '?':
85  case -1:
86  default:
87  printUsage();
88  return 1;
89  break;
90  }
91  opt = getopt_long (argc, argv, optString, longOpts, &longIndex);
92  }
93 
94  if (!rOpt || !jOpt || !tOpt || !kOpt || !vOpt) {
95  printUsage ();
96  return 1;
97  }
98 
99  return 0;
100 }
101 
102 TxLogging::TxEventLog::Level getLevel (const std::string& level) {
103  int intLevel = std::atoi (level.c_str ());
104  switch (intLevel) {
105  case 1:
106  return TxLogging::TxEventLog::LEVEL_TRACE;
107 
108  case 2:
109  return TxLogging::TxEventLog::LEVEL_DEBUG;
110 
111  case 3:
112  return TxLogging::TxEventLog::LEVEL_INFO;
113 
114  case 4:
115  return TxLogging::TxEventLog::LEVEL_NOTICE;
116 
117  case 5:
118  return TxLogging::TxEventLog::LEVEL_WARNING;
119 
120  case 6:
121  return TxLogging::TxEventLog::LEVEL_ERROR;
122 
123  case 7:
124  return TxLogging::TxEventLog::LEVEL_CRITICAL;
125 
126  case 8:
127  return TxLogging::TxEventLog::LEVEL_ALERT;
128 
129  case 9:
130  return TxLogging::TxEventLog::LEVEL_FATAL;
131 
132  default:
133  return TxLogging::TxEventLog::LEVEL_UNKNOWN;
134  }
135 }
136 
137 int main (int argc, char** argv) {
138  std::string requester = "",
139  brokerJobID = "",
140  brokerTaskID = "",
141  key = "",
142  value = "",
143  context = "",
144  level = "";
145 
146  if (getOpts (argc, argv, requester,
147  brokerJobID, brokerTaskID,
148  key, value,
149  context, level)) {
150  return 1;
151  }
152 
153  TxLogging::TxEventLog* log = TxLogging::TxEventLogFactory::create("ucm");
154  int intBJobID = std::atoi (brokerJobID.c_str ());
155  log->setBrokerJobID (intBJobID);
156  log->setBrokerTaskID (brokerTaskID);
157  log->setRequesterName (requester);
158  log->logEvent (key, value,
159  getLevel (level), TxLogging::TxEventLog::STATUS,
160  context);
161 
162  return 0;
163 }
virtual void setBrokerTaskID(const std::string &brokerTaskID)=0
virtual void setRequesterName(const std::string &requester)=0
virtual void setBrokerJobID(int brokerJobID)=0
virtual void logEvent(const std::string &logMsg, Level level=LEVEL_INFO, Stage stage=STATUS, const std::string &msgContext=TxUCMConstants::defaultContext)=0