-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from codebrainz/upstream-sync
Sync with latest upstream
- Loading branch information
Showing
11 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from geany import glog | ||
from traceback import format_exception | ||
import logging | ||
import sys | ||
|
||
|
||
GLIB_LOG_LEVEL_MAP = { | ||
logging.DEBUG: glog.LOG_LEVEL_DEBUG, | ||
logging.INFO: glog.LOG_LEVEL_INFO, | ||
logging.WARNING: glog.LOG_LEVEL_WARNING, | ||
# error and critical levels are swapped on purpose because | ||
# GLib interprets CRITICAL less fatal than Python and additionally | ||
# GLib abort()s the program on G_LOG_LEVEL_ERROR which is uncommon | ||
# in Python | ||
logging.ERROR: glog.LOG_LEVEL_CRITICAL, | ||
logging.CRITICAL: glog.LOG_LEVEL_ERROR, | ||
} | ||
|
||
|
||
class PluginLogger(object): | ||
""" | ||
Convenience wrapper softly emulating Python's logging interface. | ||
Any log messages are passed to the GLib log system using g_log() | ||
and the LOG_DOMAIN is set automatically to the plugin's name for convenience. | ||
""" | ||
|
||
def __init__(self, plugin_name): | ||
self._log_domain = u'geanypy-%s' % plugin_name | ||
|
||
def debug(self, msg_format, *args, **kwargs): | ||
self.log(logging.DEBUG, msg_format, *args, **kwargs) | ||
|
||
def info(self, msg_format, *args, **kwargs): | ||
self.log(logging.INFO, msg_format, *args, **kwargs) | ||
|
||
def message(self, msg_format, *args, **kwargs): | ||
self.log(glog.LOG_LEVEL_MESSAGE, msg_format, *args, **kwargs) | ||
|
||
def warning(self, msg_format, *args, **kwargs): | ||
self.log(logging.WARNING, msg_format, *args, **kwargs) | ||
|
||
def error(self, msg_format, *args, **kwargs): | ||
self.log(logging.ERROR, msg_format, *args, **kwargs) | ||
|
||
def exception(self, msg_format, *args, **kwargs): | ||
kwargs['exc_info'] = True | ||
self.error(msg_format, *args, **kwargs) | ||
|
||
def critical(self, msg_format, *args, **kwargs): | ||
self.log(logging.CRITICAL, msg_format, *args, **kwargs) | ||
|
||
warn = warning | ||
fatal = critical | ||
|
||
def log(self, level, msg_format, *args, **kwargs): | ||
# map log level from Python to GLib | ||
glib_log_level = GLIB_LOG_LEVEL_MAP.get(level, glog.LOG_LEVEL_MESSAGE) | ||
# format message | ||
log_message = msg_format % args | ||
# log exception information if requested | ||
exc_info = kwargs.get('exc_info', False) | ||
if exc_info: | ||
traceback_text = self._format_exception(exc_info) | ||
log_message = '%s\n%s' % (log_message, traceback_text) | ||
|
||
glog.glog(self._log_domain, glib_log_level, log_message) | ||
|
||
def _format_exception(self, exc_info): | ||
if not isinstance(exc_info, tuple): | ||
exc_info = sys.exc_info() | ||
exc_text_lines = format_exception(*exc_info) | ||
return ''.join(exc_text_lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
#include "geanypy.h" | ||
|
||
|
||
typedef struct | ||
{ | ||
PyObject_HEAD | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#if defined(HAVE_CONFIG_H) && !defined(GEANYPY_WINDOWS) | ||
# include "config.h" | ||
#endif | ||
|
||
#include "geanypy.h" | ||
|
||
|
||
static PyObject * | ||
Glog_glog(PyObject *module, PyObject *args, PyObject *kwargs) | ||
{ | ||
static gchar *kwlist[] = { "log_domain", "log_level", "message", NULL }; | ||
gchar *log_domain, *message; | ||
GLogLevelFlags log_level; | ||
|
||
if (PyArg_ParseTupleAndKeywords(args, kwargs, "sis", kwlist, &log_domain, &log_level, &message)) | ||
{ | ||
g_log(log_domain, log_level, "%s", message); | ||
} | ||
Py_RETURN_NONE; | ||
} | ||
|
||
|
||
static | ||
PyMethodDef GlogModule_methods[] = { | ||
{ "glog", (PyCFunction) Glog_glog, METH_KEYWORDS, "Wrapper around g_log()." }, | ||
{ NULL } | ||
}; | ||
|
||
|
||
PyMODINIT_FUNC initglog(void) | ||
{ | ||
PyObject *m; | ||
|
||
m = Py_InitModule3("glog", GlogModule_methods, "GLib Log utility functions."); | ||
|
||
/* TODO: These constants are for the geany.logger.GLIB_LOG_LEVEL_MAP mapping. | ||
* It would be better to build this mapping on the C layer but how to | ||
* access the Python logging.* level constants here? */ | ||
PyModule_AddIntConstant(m, "LOG_LEVEL_DEBUG", G_LOG_LEVEL_DEBUG); | ||
PyModule_AddIntConstant(m, "LOG_LEVEL_INFO", G_LOG_LEVEL_INFO); | ||
PyModule_AddIntConstant(m, "LOG_LEVEL_MESSAGE", G_LOG_LEVEL_MESSAGE); | ||
PyModule_AddIntConstant(m, "LOG_LEVEL_WARNING", G_LOG_LEVEL_WARNING); | ||
PyModule_AddIntConstant(m, "LOG_LEVEL_ERROR", G_LOG_LEVEL_ERROR); | ||
PyModule_AddIntConstant(m, "LOG_LEVEL_CRITICAL", G_LOG_LEVEL_CRITICAL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters