StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TxEventLogFactory.cxx
1 #include <sstream>
2 #include <iostream>
3 #include <fstream>
4 #include <regex.h>
5 #include <cstring>
6 
7 #include "TxEventLogFactory.h"
8 #include "TxEventLogWeb.h"
9 #include "TxEventLogCollector.h"
10 
11 using namespace TxLogging;
12 
13 //_________________________________________________________________
14 TxEventLog* TxEventLogFactory::create(const char *technology)
15 {
16  TxEventLog *log = 0;
17  if (technology && (*technology=='w' || *technology=='W' ))
18  log= new TxEventLogWeb;
19  else if (technology && (*technology=='c' || *technology=='C'|| *technology=='U' || *technology=='u' ))
20  log= new TxEventLogCollector;
21  else
22  log=new TxEventLogFile;
23  return log;
24 }
25 
26 using namespace std;
27 namespace {
53  //________________________________________________________________________
54 static void log(const char *key, const char *value)
55 {
56  using namespace TxLogging;
57  printf("\" %s \"=\"%s\"", key, value);
58 
59  const char * ucmCollector = "ucm";
60  TxEventLog *eventLog = TxEventLogFactory::create(ucmCollector);
61 
62  //Find magic keys
63  if(! strcmp(key,"JobSubmitState")){
64 
65  if(!strcmp(value, "STAGEIN")){
66  eventLog->setJobSubmitState(TxEventLog::STAGEIN);
67  }else if(!strcmp(value,"ACTIVE")){
68  eventLog->setJobSubmitState(TxEventLog::ACTIVE);
69  }else if(!strcmp(value, "DONE")){
70  eventLog->setJobSubmitState(TxEventLog::DONE);
71  }
72 
73  }else{
74  //If it's not a magic work, just add it as a key value pair
75  eventLog->logStart(key, value);
76  }
77 }
78 //________________________________________________________________________
79 int Main(int argc, const char *argv[])
80 {
81  string arg;
82  while (--argc) arg += *(++argv);
83  regex_t rgex1;
84  regex_t rgex2;
85  char errtext[512]={0};
86  int errcode = 0;
87 
88  const char *regX1 = "^ *([^ ]+) *= *([^ ]+.*[^ ]*) *$";
89  const char *regX2 = "^ *-key *([^ ]+) *-value *([^ ]+.*[^ ]*) *$";
90  regcomp(&rgex1,regX1,REG_ICASE | REG_EXTENDED);
91  regcomp(&rgex2,regX2,REG_ICASE | REG_EXTENDED );
92  regmatch_t matchptr [3];
93  size_t nmatch = sizeof(matchptr)/sizeof(regmatch_t);
94  if (! (errcode = regexec(&rgex1,arg.c_str(),nmatch,matchptr,0) ) ) {
95  string key = arg.substr(matchptr[1].rm_so, matchptr[1].rm_eo-matchptr[1].rm_so );
96  string value = arg.substr(matchptr[2].rm_so, matchptr[2].rm_eo-matchptr[2].rm_so );
97  log(key.c_str(), value.c_str());
98  } else if (! regexec(&rgex2,arg.c_str(),nmatch,matchptr,0) ) {
99  string key = arg.substr(matchptr[1].rm_so, matchptr[1].rm_eo-matchptr[1].rm_so );
100  string value = arg.substr(matchptr[2].rm_so, matchptr[2].rm_eo-matchptr[2].rm_so );
101  log(key.c_str(), value.c_str());
102  } else{
103  const char *usage = " Input format error\n"
104  "\n"
105  "Usage:\n"
106  "\n"
107  " ulog -key [key] -value [value]\n"
108  "or\n"
109  " ulog [key]=[value]\n"
110  "\n";
111  printf("\n%s: %s\n",arg.c_str(),usage);
112  return 1;
113  }
114  regfree(&rgex1);
115  regfree(&rgex2);
116  return 0;
117  }
118 }
119 
120 //_________________________________________________________________
121 int TxEventLogFactory::main(int argc, const char *argv[])
122 {
123  return Main(argc, argv);
124 }