from server import process_id, Server
import json
import subprocess
+import sendTraces
# -----------------------------------------------------------------------------
print logfile
print "==========================================================="
self.CMD=['SALOME_Logger_Server', logfile]
+ self.logfile = logfile
pass
pass # end of LoggerServer class
# and wait until it is registered in naming service
#
+ sendTraces.logfile=None
if args['logger']:
myServer=LoggerServer(args)
myServer.run()
+ sendTraces.logfile=myServer.logfile
clt.waitLogger("Logger")
# set siman python path before the session server launching to import scripts inside python console
from killSalomeWithPort import killMyPort
killMyPort(port)
pass
+ if not sendTraces.logfile is None:
+ try:
+ sendTraces.sendSalome(sendTraces.logfile)
+ except Exception as err:
+ print "sendTraces failed:"
+ print err
return
#
--- /dev/null
+#!/usr/bin/env python
+
+"""
+ This module is used to send execution traces to a server.
+"""
+
+import sys
+import os
+import json
+import urllib2
+import urllib
+
+class TracesSender(object):
+ """
+ Collect and send STUC traces to a server.
+ """
+
+ def loadLogFile(self, filePath):
+ """
+ Load STUC traces from a log file.
+
+ STUC traces have this format:
+ [STUC TRACE]{ "label":"name","begin":"2014-07-24 14:18:45","end":"2014-07-24 14:18:47"}
+
+ Lines which don't containt the flag "[STUC TRACE]" are ignored.
+ Between [STUC TRACE] and the end of the line, there should be
+ the trace data in json format.
+ """
+ self.traces = []
+ logFile = open(filePath)
+ flag = '[STUC TRACE]'
+ for line in logFile:
+ x, y, p = line.partition(flag)
+ if p != '':
+ trace = json.loads(p)
+ self.traces.append(trace)
+ pass
+ logFile.close()
+ pass
+
+ def setInfo(self, appName, appVersion, installPath):
+ """
+ Informations to join to the traces.
+
+ :param appName: name of the current application
+ :param appVersion: version of the current application
+ :param installPath: installation path of the current application
+ """
+ self.appName = appName
+ self.appVersion = appVersion
+ self.installPath = installPath
+ pass
+
+ def send(self, serverUrl):
+ """
+ Send STUC traces to a server.
+
+ :param serverUrl: complet url where the traces should be sent
+ """
+ session = {'appName' : self.appName,
+ 'appVersion' : self.appVersion,
+ 'installPath': self.installPath,
+ 'traces' : json.dumps(self.traces)}
+ dataToSend = urllib.urlencode(session)
+# headers = {"Content-type": "application/x-www-form-urlencoded",
+# "Accept": "text/plain"}
+# req = urllib2.Request(serverUrl, dataToSend,headers )
+ req = urllib2.Request(serverUrl, dataToSend)
+ response = urllib2.urlopen(req)
+ return response
+
+ pass
+
+def sendSalome(logfilepath):
+ """
+ TracesSender usecase for SALOME
+ """
+ ts = TracesSender()
+ salome_path = os.getenv("ROOT_SALOME")
+ kernel_path = os.getenv('KERNEL_ROOT_DIR', '' )
+ version_path = os.path.join(kernel_path, "bin", "salome", "VERSION")
+ version_file = open(version_path)
+ version = "unknown"
+ for line in version_file:
+ x, y, p = line.partition("[SALOME KERNEL] :")
+ if p != '':
+ version = p
+ break
+ pass
+ pass
+
+ ts.setInfo("SALOME", version, salome_path)
+ ts.loadLogFile(logfilepath)
+ ts.send("http://localhost:8000/cgi-bin/traces.py")
+
+### MAIN ##
+if __name__ == "__main__":
+ try:
+ serverUrl = sys.argv[1]
+ appName = sys.argv[2]
+ appVersion = sys.argv[3]
+ installPath = sys.argv[4]
+ logFile = sys.argv[5]
+ except:
+ print "usage:"
+ print " sendTraces.py <serverUrl> <appName> <appVersion> <installPath> <logFile>"
+ exit()
+ pass
+
+ ts = TracesSender()
+ ts.setInfo(appName, appVersion, installPath)
+ ts.loadLogFile(logFile)
+ ts.send(serverUrl)
--- /dev/null
+#include "StucTrace.hxx"
+#include <ctime>
+#include <iostream>
+
+#include "LocalTraceBufferPool.hxx"
+
+StucTrace::StucTrace(const std::string& label)
+: _label(label),
+ _beginTime(),
+ _endTime(),
+ _trace()
+{
+ begin();
+}
+
+StucTrace::~StucTrace()
+{
+}
+
+void StucTrace::begin()
+{
+ time_t now = time(0);
+ struct tm tstruct = *localtime(&now);
+ strftime(_beginTime, sizeof(_beginTime), "%Y-%m-%d %X", &tstruct);
+}
+
+void StucTrace::end()
+{
+ time_t now = time(0);
+ struct tm tstruct = *localtime(&now);
+ strftime(_endTime, sizeof(_endTime), "%Y-%m-%d %X", &tstruct);
+
+ print();
+}
+
+const std::string& StucTrace::getTrace()const
+{
+ _trace = "[STUC TRACE]{\"label\":\"";
+ _trace += _label + "\",\"begin\":\"";
+ _trace += _beginTime;
+ _trace += "\",\"end\":\"";
+ _trace += _endTime;
+ _trace += "\"}";
+
+ return _trace;
+}
+
+void StucTrace::print()const
+{
+ LocalTraceBufferPool::instance()->insert(NORMAL_MESS,(getTrace()+"\n").c_str());
+}
--- /dev/null
+#ifndef __STUC__TRACE__HXX__
+#define __STUC__TRACE__HXX__
+
+#include <string>
+
+/*!
+ * Print a timestamped and labeled trace.
+ * These traces will be recognised by sendTraces.py.
+ */
+class StucTrace
+{
+ public:
+ /*!
+ * Save the label and the begin time.
+ * The begin time can be changed later by begin().
+ */
+ StucTrace(const std::string& label);
+
+ virtual ~StucTrace();
+
+ /*!
+ * Reset the begin time
+ */
+ void begin();
+
+ /*!
+ * Save the end time and print the trace
+ */
+ void end();
+
+ /*!
+ * Get the formated trace.
+ */
+ const std::string& getTrace()const;
+
+ /*!
+ * Overload this method in your derived class in order to define where
+ * the trace should be written.
+ */
+ virtual void print()const;
+
+ private:
+ std::string _label;
+ char _beginTime[64];
+ char _endTime[64];
+ mutable std::string _trace;
+};
+
+#define STUC_TRACE(label) std::ostringstream os; os << label; StucTrace t(os.str().c_str()); t.print();
+
+#endif
\ No newline at end of file