TreeFrogFramework  2.8
tcommandlineinterface.h
Go to the documentation of this file.
1 #pragma once
2 #include "tkvsdatabasepool.h"
3 #include "tsqldatabasepool.h"
4 #include <TActionThread>
5 #include <TSystemGlobal>
6 #include <TStdErrSystemLogger>
7 #include <TStdOutLogger>
8 #include <TWebApplication>
9 #include <TAppSettings>
10 #include <TLogger>
11 #include <QtCore>
12 
13 
14 #if QT_VERSION < 0x060000
15 #define TF_CLI_MAIN(STATICFUNCTION) \
16  int main(int argc, char *argv[]) \
17  { \
18  class Thread : public TActionThread { \
19  public: \
20  Thread() : TActionThread(0), returnCode(0) { } \
21  volatile int returnCode; \
22  \
23  protected: \
24  virtual void run() \
25  { \
26  returnCode = STATICFUNCTION(); \
27  commitTransactions(); \
28  for (auto it = sqlDatabases.begin(); it != sqlDatabases.end(); ++it) { \
29  it.value().database().close(); /* close SQL database */ \
30  } \
31  for (auto it = kvsDatabases.begin(); it != kvsDatabases.end(); ++it) { \
32  it.value().close(); /* close KVS database */ \
33  } \
34  QEventLoop eventLoop; \
35  while (eventLoop.processEvents()) { } \
36  } \
37  }; \
38  TWebApplication app(argc, argv); \
39  QByteArray codecName = Tf::appSettings()->value(Tf::InternalEncoding).toByteArray(); \
40  QTextCodec *codec = QTextCodec::codecForName(codecName); \
41  QTextCodec::setCodecForLocale(codec); \
42  Tf::setupSystemLogger(new TStdErrSystemLogger); \
43  Tf::setupQueryLogger(); \
44  app.setMultiProcessingModule(TWebApplication::Thread); \
45  int idx = QCoreApplication::arguments().indexOf("-e"); \
46  QString env = (idx > 0) ? QCoreApplication::arguments().value(idx + 1) : QString("product"); \
47  app.setDatabaseEnvironment(env); \
48  Thread thread; \
49  QObject::connect(&thread, SIGNAL(finished()), &app, SLOT(quit())); \
50  thread.start(); \
51  app.exec(); \
52  Tf::releaseAppLoggers(); \
53  Tf::releaseQueryLogger(); \
54  return thread.returnCode; \
55  }
56 
57 #else
58 
59 #define TF_CLI_MAIN(STATICFUNCTION) \
60  int main(int argc, char *argv[]) \
61  { \
62  class Thread : public TActionThread { \
63  public: \
64  Thread() : TActionThread(0), returnCode(0) { } \
65  volatile int returnCode; \
66  \
67  protected: \
68  virtual void run() \
69  { \
70  returnCode = STATICFUNCTION(); \
71  commitTransactions(); \
72  for (auto it = sqlDatabases.begin(); it != sqlDatabases.end(); ++it) { \
73  it.value().database().close(); /* close SQL database */ \
74  } \
75  for (auto it = kvsDatabases.begin(); it != kvsDatabases.end(); ++it) { \
76  it.value().close(); /* close KVS database */ \
77  } \
78  QEventLoop eventLoop; \
79  while (eventLoop.processEvents()) { } \
80  } \
81  }; \
82  TWebApplication app(argc, argv); \
83  Tf::setupSystemLogger(new TStdErrSystemLogger); \
84  Tf::setupQueryLogger(); \
85  app.setMultiProcessingModule(TWebApplication::Thread); \
86  int idx = QCoreApplication::arguments().indexOf("-e"); \
87  QString env = (idx > 0) ? QCoreApplication::arguments().value(idx + 1) : QString("product"); \
88  app.setDatabaseEnvironment(env); \
89  Thread thread; \
90  QObject::connect(&thread, SIGNAL(finished()), &app, SLOT(quit())); \
91  thread.start(); \
92  app.exec(); \
93  Tf::releaseAppLoggers(); \
94  Tf::releaseQueryLogger(); \
95  return thread.returnCode; \
96  }
97 #endif