]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr: redirect python stdout,stderr to ceph log
authorKefu Chai <kchai@redhat.com>
Tue, 28 Mar 2017 10:49:01 +0000 (18:49 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 21 Apr 2017 06:17:09 +0000 (14:17 +0800)
PyErr_Print() uses a file object "stderr" for printing messages.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/PyModules.cc
src/mgr/PyModules.h

index 6bb5384ec9bea31f718d742f15d966ed17796410..e77df011a8c8875de92aac494e439c677ee239e0 100644 (file)
 
 #define dout_context g_ceph_context
 #define dout_subsys ceph_subsys_mgr
+
+#undef dout_prefix
+#define dout_prefix *_dout << "mgr[py] "
+
+namespace {
+  PyObject* log_write(PyObject*, PyObject* args) {
+    char* m = nullptr;
+    if (PyArg_ParseTuple(args, "s", &m)) {
+      auto len = strlen(m);
+      if (len && m[len-1] == '\n') {
+       m[len-1] = '\0';
+      }
+      dout(4) << m << dendl;
+    }
+    Py_RETURN_NONE;
+  }
+
+  PyObject* log_flush(PyObject*, PyObject*){
+    Py_RETURN_NONE;
+  }
+
+  static PyMethodDef log_methods[] = {
+    {"write", log_write, METH_VARARGS, "write stdout and stderr"},
+    {"flush", log_flush, METH_VARARGS, "flush"},
+    {nullptr, nullptr, 0, nullptr}
+  };
+}
+
 #undef dout_prefix
 #define dout_prefix *_dout << "mgr " << __func__ << " "
 
@@ -363,7 +391,17 @@ int PyModules::init()
   // fake one.  This step also picks up site-packages into sys.path.
   const char *argv[] = {"ceph-mgr"};
   PySys_SetArgv(1, (char**)argv);
-  
+
+  if (g_conf->daemonize) {
+    auto py_logger = Py_InitModule("ceph_logger", log_methods);
+#if PY_MAJOR_VERSION >= 3
+    PySys_SetObject("stderr", py_logger);
+    PySys_SetObject("stdout", py_logger);
+#else
+    PySys_SetObject(const_cast<char*>("stderr"), py_logger);
+    PySys_SetObject(const_cast<char*>("stdout"), py_logger);
+#endif
+  }
   // Populate python namespace with callable hooks
   Py_InitModule("ceph_state", CephStateMethods);
 
index 7ee2a494f6a0de105c127736e7a8c593b2199d62..6c6361a812592409a4cb560c480e0bd13036db3b 100644 (file)
@@ -29,7 +29,6 @@ class PyModules
 {
   std::map<std::string, std::unique_ptr<MgrPyModule>> modules;
   std::map<std::string, std::unique_ptr<ServeThread>> serve_threads;
-
   DaemonStateIndex &daemon_state;
   ClusterState &cluster_state;
   MonClient &monc;