From fc35a8210c4fa4f5b20b8ef30d351936a1faa35e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Tue, 6 Jul 2021 14:54:12 +0200
Subject: [PATCH] added logging

---
 README.md            | 6 +++++-
 docs/usage.rst       | 6 ++++++
 setup.cfg            | 2 +-
 tempsdb/database.pxd | 2 +-
 tempsdb/database.pyx | 8 ++++++++
 tempsdb/series.pyx   | 7 ++++++-
 tempsdb/varlen.pyx   | 6 +++++-
 7 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index a08ea05..4abcbb1 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,7 @@ though binary wheels for:
 * Linux [manylinux2014]:
     * x86_64:
         * Python 3.8
+        * Python 3.9
     * armv7l:
         * Python 3.8
 
@@ -47,11 +48,14 @@ python setup.py bdist_wheel
 
 Then copy your resulting wheel and install it via pip on the target system.
 
+Be aware that tempsdb does logging. If necessary, setting log level of 
+logger `tempsdb` to WARN will eliminate all warnings that tempsdb outputs.
+
 # Changelog
 
 ## v0.6.3
 
-* _TBA_
+* added logging for opening and closing series
 
 ## v0.6.2
 
diff --git a/docs/usage.rst b/docs/usage.rst
index a535da6..4c3876b 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -48,3 +48,9 @@ Appending the data is done via :meth:`~tempsdb.series.TimeSeries.append`. Since
 allocated in entire pages, so your files will be padded to a page in size. This makes writes
 quite fast, as in 99.9% cases it is just a memory operation.
 
+Logging
+-------
+
+tempsdb will log when opening and closing series. To prevent this from happening, just call:
+
+.. autofunction:: tempsdb.database.disable_logging
diff --git a/setup.cfg b/setup.cfg
index 4e4a4c0..8f3ac77 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,7 +1,7 @@
 # coding: utf-8
 [metadata]
 name = tempsdb
-version = 0.6.3a1
+version = 0.6.3
 long-description = file: README.md
 long-description-content-type = text/markdown; charset=UTF-8
 license_files = LICENSE
diff --git a/tempsdb/database.pxd b/tempsdb/database.pxd
index 24594a7..f6b4df7 100644
--- a/tempsdb/database.pxd
+++ b/tempsdb/database.pxd
@@ -38,4 +38,4 @@ cdef class Database:
     cpdef int sync(self) except -1
 
 cpdef Database create_database(str path)
-
+cpdef int disable_logging() except -1
diff --git a/tempsdb/database.pyx b/tempsdb/database.pyx
index 7391449..b714651 100644
--- a/tempsdb/database.pyx
+++ b/tempsdb/database.pyx
@@ -1,4 +1,5 @@
 import os
+import logging
 import shutil
 import threading
 import warnings
@@ -10,6 +11,13 @@ from .series cimport TimeSeries, create_series
 from .varlen cimport VarlenSeries
 from .metadata cimport read_meta_at, write_meta_at
 
+
+cpdef int disable_logging() except -1:
+    """Disable logging of tempsdb"""
+    logging.getLogger('tempsdb').setLevel(logging.WARN)
+    return 0
+
+
 cdef class Database:
     """
     A basic TempsDB object.
diff --git a/tempsdb/series.pyx b/tempsdb/series.pyx
index b0ba398..c33cc2c 100644
--- a/tempsdb/series.pyx
+++ b/tempsdb/series.pyx
@@ -4,6 +4,7 @@ import typing as tp
 import shutil
 import threading
 import warnings
+import logging
 
 from .chunks.base cimport Chunk
 from .chunks.normal cimport NormalChunk
@@ -13,7 +14,9 @@ from .exceptions import DoesNotExist, Corruption, InvalidState, AlreadyExists
 from .metadata cimport read_meta_at, write_meta_at
 
 
-cdef set metadata_file_names = {'metadata.txt', 'metadata.minijson'}
+cdef:
+    set metadata_file_names = {'metadata.txt', 'metadata.minijson'}
+    object logger = logging.getLogger(__name__)
 
 
 cdef class TimeSeries:
@@ -96,6 +99,7 @@ cdef class TimeSeries:
         return 0
 
     def __init__(self, str path, str name, bint use_descriptor_based_access = False):
+        logger.info('Opening new time series at %s called %s', path, name)
         self.descriptor_based_access = use_descriptor_based_access
         self.mpm = None
         self.name = name
@@ -281,6 +285,7 @@ cdef class TimeSeries:
             self.mpm.cancel()
             self.mpm = None
         self.closed = True
+        logger.info('Closed time series at %s called %s', self.path, self.name)
         return 0
 
     cdef unsigned int get_index_of_chunk_for(self, unsigned long long timestamp):
diff --git a/tempsdb/varlen.pyx b/tempsdb/varlen.pyx
index 964b246..c187bc4 100644
--- a/tempsdb/varlen.pyx
+++ b/tempsdb/varlen.pyx
@@ -1,4 +1,5 @@
 import os
+import logging
 import shutil
 import typing as tp
 import struct
@@ -7,8 +8,9 @@ import warnings
 from .chunks.base cimport Chunk
 from .exceptions import Corruption, AlreadyExists, StillOpen
 from .iterators cimport Iterator
-from tempsdb.series cimport TimeSeries, create_series
+from .series cimport TimeSeries, create_series
 
+cdef object logger = logging.getLogger(__name__)
 
 cdef class VarlenEntry:
     """
@@ -495,6 +497,7 @@ cdef class VarlenSeries:
             it.close()
 
     def __init__(self, str path, str name, bint use_descriptor_based_access = False):
+        logger.info('Opening varlen series at %s called %s', path, name)
         self.closed = False
         self.mmap_enabled = not use_descriptor_based_access
         self.path = path
@@ -690,6 +693,7 @@ cdef class VarlenSeries:
         cdef TimeSeries series
         for series in self.series:
             series.close()
+        logger.info('Closed varlen series at %s called %s', self.path, self.name)
         return 0
 
     cpdef int trim(self, unsigned long long timestamp) except -1:
-- 
GitLab