[Git][NTPsec/ntpsec][master] Added dolog and tests

Ian Bruene gitlab at mg.gitlab.com
Tue Aug 8 22:03:05 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
b43ab72e by Ian Bruene at 2017-08-08T21:52:12+00:00
Added dolog and tests

- - - - -


2 changed files:

- pylib/util.py
- tests/pylib/test_util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -57,6 +57,14 @@ MS_VARS = ("rootdelay", "rootdisp", "offset", "sys_jitter", "clk_jitter",
 PPM_VARS = ("frequency", "clk_wander", "clk_wander_threshold")
 
 
+def dolog(logfp, text, level):
+    if logfp is None:
+        return  # can turn off logging by supplying a None file descriptior
+    if debug >= level:
+        logfp.write(text)
+        logfp.flush()  # we don't want to lose an important log to a crash
+
+
 def safeargcast(arg, castfunc, errtext, usage):
     """Attempts to typecast an argument, prints and dies on failure.
     errtext must contain a %s for splicing in the argument, and be


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -7,6 +7,33 @@ import ntp.util
 
 class TestPylibUtilMethods(unittest.TestCase):
 
+    def test_dolog(self):
+        f = ntp.util.dolog
+
+        # We need a test jig
+        class LogTester:
+            def __init__(self):
+                self.written = None
+                self.flushed = False
+
+            def write(self, text):
+                if self.written is None:
+                    self.written = ""
+                self.written += text
+
+            def flush(self):
+                self.flushed = True
+        # Test with logging off (fd == None)
+        #   uh... if someone can think of a way to do that please tell me
+        # Test with logging on, below threshold
+        jig = LogTester()
+        f(jig, "blah", 0)
+        self.assertEqual((jig.written, jig.flushed), (None, False))
+        # Test with logging on, above threshold
+        jig.__init__()  # reset
+        f(jig, "blah", 0)
+        self.assertEqual((jig.written, jig.flushed), ("blah", True))
+
     def test_rfc3339(self):
         self.assertEqual(ntp.util.rfc3339(1480999786),
                          '2016-12-06T04:49:46Z')



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/b43ab72e3c8e7fec4d20275d8f46492a2290ef22

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/b43ab72e3c8e7fec4d20275d8f46492a2290ef22
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170808/750f5ef7/attachment.html>


More information about the vc mailing list