Skip to content

Commit 087c788

Browse files
committed
add logging functionalities
1 parent 6e53454 commit 087c788

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

amplpy/errorhandler.pxi

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
# -*- coding: utf-8 -*-
2+
import logging
3+
24
try:
35
from .tools import _SUPPORT_MESSAGE
46
except Exception:
57
_SUPPORT_MESSAGE = ""
68

9+
logger = logging.getLogger("amplpy")
10+
logger.setLevel(logging.WARNING)
11+
if not logger.hasHandlers():
12+
handler = logging.StreamHandler()
13+
formatter = logging.Formatter("[%(levelname)s] %(message)s")
14+
handler.setFormatter(formatter)
15+
logger.addHandler(handler)
16+
logger.propagate = False
17+
718
def display_error_message(exception, error=True):
8-
msg = "\t" + str(exception).replace("\n", "\n\t")
19+
logger.setLevel(logging.WARNING)
20+
msg = "\t" + str(exception).replace(_SUPPORT_MESSAGE, "").replace("\n", "\n\t")
921
if error:
10-
print(f"Error:\n{msg}{_SUPPORT_MESSAGE}")
22+
logger.error(f"\n{msg}")
1123
else:
12-
print(f"Warning:\n{msg}")
24+
logger.warning(f"\n{msg}")
1325

1426

1527
cdef class ErrorHandler:
@@ -38,8 +50,9 @@ cdef class ErrorHandler:
3850

3951
cdef void PyError(bool_c isWarning, const char* filename, int row, int offset, const char* message, void* errorHandler) except * with gil:
4052
handler = <ErrorHandler>errorHandler
41-
exception = AMPLException(filename.decode('utf-8'), row, offset, message.decode('utf-8'))
4253
if isWarning:
54+
exception = AMPLException(filename.decode('utf-8'), row, offset, message.decode('utf-8'))
4355
handler.warning(exception)
4456
else:
57+
exception = AMPLException(filename.decode('utf-8'), row, offset, f"{message.decode('utf-8')}{_SUPPORT_MESSAGE}")
4558
handler.error(exception)

0 commit comments

Comments
 (0)