Skip to content

Commit

Permalink
CHANGED more fprintf(stderr, ...) to LOG(ERROR)
Browse files Browse the repository at this point in the history
ADDED redirection of python sys.stderr to LOG(ERROR)
  • Loading branch information
ksterker committed Jul 1, 2012
1 parent d2ff169 commit 88c9bb8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/base/logging.cc
Expand Up @@ -29,6 +29,14 @@

#include <string>

namespace base
{
void stderr_to_log::write (const char *msg)
{
LOG(ERROR) << msg;
}
}

namespace google
{
int log_level = 3;
Expand Down
21 changes: 21 additions & 0 deletions src/base/logging.h
Expand Up @@ -28,6 +28,26 @@
#ifndef LOGGING_H_
#define LOGGING_H_

namespace base
{
/*
* Redirect Python stderr to our own logging
* implementation. For this to work, an instance
* of this class has to be assigned to sys.stderr.
*/
class stderr_to_log
{
public:
/*
* Redirect message to log, using level ERROR.
* @param msg the message to append to the log.
*/
void write (const char *msg);
};
}

#ifndef SWIG

#include <iostream> // provide std::endl
#include <string>
#include <cstdlib>
Expand Down Expand Up @@ -122,3 +142,4 @@ namespace logging
} // namespace{}

#endif
#endif
1 change: 1 addition & 0 deletions src/py-wrappers/adonthell/py_base.i
Expand Up @@ -170,6 +170,7 @@ namespace base {
typedef long time_t;
%include "base/savegame.h"
%include "base/serializer.h"
%include "base/logging.h"

%template(py_serializer) base::serializer<PyObject>;

Expand Down
15 changes: 8 additions & 7 deletions src/python/python.cc
@@ -1,6 +1,4 @@
/*
$Id: python.cc,v 1.15 2009/04/16 21:06:09 ksterker Exp $
Copyright (C) 2003/2004 Alexandre Courbot <alexandrecourbot@linuxgames.com>
Part of the Adonthell Project http://adonthell.linuxgames.com
Expand Down Expand Up @@ -48,7 +46,6 @@ namespace python
// PyErr_Print will clear the error state, so it must
// be called after the checks above.
PyErr_Print ();
fflush (stderr);
}
}

Expand All @@ -57,6 +54,9 @@ namespace python
{
Py_Initialize ();
pool::init ();

// make sure that PyErr_Print uses our own logging
run_simple_string("import sys; from adonthell.base import stderr_to_log; sys.stderr = stderr_to_log();");
}

// shutdown python interpreter
Expand Down Expand Up @@ -117,7 +117,7 @@ namespace python
// make sure the given arguments are a tuple
if (tuple && !PyTuple_Check (tuple))
{
fprintf (stderr, "*** error: python::pad_tuple: argument must be a tuple!\n");
LOG(ERROR) << "python::pad_tuple: argument must be a tuple!";
return NULL;
}

Expand Down Expand Up @@ -174,8 +174,9 @@ namespace python
}
default:
{
fprintf (stderr, "*** python::get_tuple: unsupported type: %s!\n",
base::flat::name_for_type ((base::flat::data_type) type));
LOG(ERROR) << "python::get_tuple: unsupported type: "
<< base::flat::name_for_type ((base::flat::data_type) type)
<< "!";
break;
}
}
Expand Down Expand Up @@ -210,7 +211,7 @@ namespace python
else
{
PyObject *repr = PyObject_Repr (item);
fprintf (stderr, "*** python::put_tuple: cannot save '%s'!\n", PyString_AsString (repr));
LOG(ERROR) << "python::put_tuple: cannot save '" << PyString_AsString (repr) << "'!";
Py_DECREF (repr);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/python/script.cc
Expand Up @@ -137,7 +137,7 @@ PyObject* script::call_method_ret (const string &name, PyObject *args) const
}
else
{
fprintf (stderr, "*** script::call_method_ret: '%s' is not callable!\n", name.c_str ());
LOG(ERROR) << "script::call_method_ret: '" << name << "' is not callable!";
}
}

Expand Down

0 comments on commit 88c9bb8

Please sign in to comment.