Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/picologging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ def setLoggerClass(self, klass):
self.cls = klass

def setLogRecordFactory(self, factory):
raise NotImplementedError("setLogRecordFactory is not supported in picologging.")
raise NotImplementedError(
"setLogRecordFactory is not supported in picologging."
)


root = Logger(name="root", level=WARNING)
Expand Down
13 changes: 9 additions & 4 deletions src/picologging/formatter.cxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ctime>
#include <charconv>
#include "picologging.hxx"
#include "formatter.hxx"
#include "formatstyle.hxx"
Expand Down Expand Up @@ -90,15 +91,19 @@ PyObject* Formatter_format(Formatter *self, PyObject *record){
}
if (self->usesTime){
PyObject * asctime = Py_None;
std::time_t created = (std::time_t)logRecord->created;
double createdInt;
int createdFrac = std::modf(logRecord->created, &createdInt) * 1e3;
std::time_t created = static_cast<std::time_t>(createdInt);
std::tm *ct = localtime(&created);
if (self->dateFmt != Py_None){
char buf[100];
size_t len = strftime(buf, 100, self->dateFmtStr, ct);
size_t len = strftime(buf, sizeof(buf), self->dateFmtStr, ct);
asctime = PyUnicode_FromStringAndSize(buf, len);
} else {
char buf[100];
asctime = PyUnicode_FromFormat("%s,%03d", buf, logRecord->msecs);
size_t len = strftime(buf, sizeof(buf), "%F %T" , ct);
len += snprintf(buf + len, sizeof(buf) - len, ",%03d", createdFrac);
asctime = PyUnicode_FromStringAndSize(buf, len);
}

Py_XDECREF(logRecord->asctime);
Expand Down Expand Up @@ -349,4 +354,4 @@ PyTypeObject FormatterType = {
0, /* tp_alloc */
Formatter_new, /* tp_new */
PyObject_Del, /* tp_free */
};
};
13 changes: 13 additions & 0 deletions tests/unit/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import sys
import traceback
from logging import Formatter as LoggingFormatter

import pytest
from utils import filter_gc
Expand Down Expand Up @@ -147,6 +148,18 @@ def test_asctime_field():
assert pico_f.usesTime()


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_asctime_field_buffer():
pico_f = Formatter("%(asctime)s")
record = LogRecord(
"hello", logging.WARNING, __file__, 123, "bork bork bork", (), None
)
logging_f = LoggingFormatter("%(asctime)s")

assert pico_f.format(record).split(",")[0] == logging_f.format(record).split(",")[0]
assert pico_f.usesTime()


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_record_with_stack_info():
pico_f = Formatter("%(message)s")
Expand Down
Loading