1 module htslib.hts_log;
2 import std..string: toStringz;
3 
4 /// \file htslib/hts_log.h
5 /// Configuration of log levels.
6 /* The MIT License
7 Copyright (C) 2017 Genome Research Ltd.
8 
9 Author: Anders Kaplan
10 
11 Permission is hereby granted, free of charge, to any person obtaining
12 a copy of this software and associated documentation files (the
13 "Software"), to deal in the Software without restriction, including
14 without limitation the rights to use, copy, modify, merge, publish,
15 distribute, sublicense, and/or sell copies of the Software, and to
16 permit persons to whom the Software is furnished to do so, subject to
17 the following conditions:
18 
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30 */
31 
32 extern(C):
33 
34 /// Log levels.
35 enum htsLogLevel {          // @suppress(dscanner.style.phobos_naming_convention)
36     HTS_LOG_OFF,            ///< All logging disabled.
37     HTS_LOG_ERROR,          ///< Logging of errors only.
38     HTS_LOG_WARNING = 3,    ///< Logging of errors and warnings.
39     HTS_LOG_INFO,           ///< Logging of errors, warnings, and normal but significant events.
40     HTS_LOG_DEBUG,          ///< Logging of all except the most detailed debug events.
41     HTS_LOG_TRACE           ///< All logging enabled.
42 }
43 
44 /// Sets the selected log level.
45 void hts_set_log_level(htsLogLevel level);
46 
47 /// Gets the selected log level.
48 htsLogLevel hts_get_log_level();
49 
50 /// Selected log level.
51 /*!
52  * One of the HTS_LOG_* values. The default is HTS_LOG_WARNING.
53  * \note Avoid direct use of this variable. Use hts_set_log_level and hts_get_log_level instead.
54  */
55 extern __gshared int hts_verbose;
56 
57 /**! Logs an event.
58 * \param severity      Severity of the event:
59 *                      - HTS_LOG_ERROR means that something went wrong so that a task could not be completed.
60 *                      - HTS_LOG_WARNING means that something unexpected happened, but that execution can continue, perhaps in a degraded mode.
61 *                      - HTS_LOG_INFO means that something normal but significant happened.
62 *                      - HTS_LOG_DEBUG means that something normal and insignificant happened.
63 *                      - HTS_LOG_TRACE means that something happened that might be of interest when troubleshooting.
64 * \param context       Context where the event occurred. Typically set to "__func__".
65 * \param format        Format string with placeholders, like printf.
66 */
67 void hts_log(htsLogLevel severity, const(char) *context, const(char) *format, ...);
68 
69 pragma(inline, true):
70 /**! Logs an event with severity HTS_LOG_ERROR and default context. Parameters: format, ... */
71 //#define hts_log_error(...) hts_log(HTS_LOG_ERROR, __func__, __VA_ARGS__)
72 void hts_log_error(const(char)[] ctx, string msg)
73 {
74     string colormsg = "\x1b[0;31m" ~ msg ~ "\x1b[0m";
75     hts_log(htsLogLevel.HTS_LOG_ERROR, toStringz(ctx), toStringz(colormsg));
76 }
77 /**! Logs an event with severity HTS_LOG_WARNING and default context. Parameters: format, ... */
78 //#define hts_log_warning(...) hts_log(HTS_LOG_WARNING, __func__, __VA_ARGS__)
79 void hts_log_warning(const(char)[] ctx, string msg)
80 {
81     string colormsg = "\x1b[0;33m" ~ msg ~ "\x1b[0m";
82     hts_log(htsLogLevel.HTS_LOG_WARNING, toStringz(ctx), toStringz(colormsg));
83 }
84 
85 /**! Logs an event with severity HTS_LOG_INFO and default context. Parameters: format, ... */
86 //#define hts_log_info(...) hts_log(HTS_LOG_INFO, __func__, __VA_ARGS__)
87 void hts_log_info(const(char)[] ctx, string msg)
88 {
89     string colormsg = "\x1b[0;32m" ~ msg ~ "\x1b[0m";
90     hts_log(htsLogLevel.HTS_LOG_INFO, toStringz(ctx), toStringz(colormsg));
91 }
92 
93 /**! Logs an event with severity HTS_LOG_DEBUG and default context. Parameters: format, ... */
94 //#define hts_log_debug(...) hts_log(HTS_LOG_DEBUG, __func__, __VA_ARGS__)
95 void hts_log_debug(const(char)[] ctx, string msg)
96 {
97     string colormsg = "\x1b[0;36m" ~ msg ~ "\x1b[0m";
98     hts_log(htsLogLevel.HTS_LOG_DEBUG, toStringz(ctx), toStringz(colormsg));
99 }
100 
101 /**! Logs an event with severity HTS_LOG_TRACE and default context. Parameters: format, ... */
102 //#define hts_log_trace(...) hts_log(HTS_LOG_TRACE, __func__, __VA_ARGS__)
103 void hts_log_trace(const(char)[] ctx, string msg)
104 {
105     string colormsg = "\x1b[1;36m" ~ msg ~ "\x1b[0m";
106     hts_log(htsLogLevel.HTS_LOG_TRACE, toStringz(ctx), toStringz(colormsg));
107 }
108 debug(dhtslib_unittest)
109 unittest{
110     hts_set_log_level(htsLogLevel.HTS_LOG_TRACE);
111     hts_log_trace(__FUNCTION__, "Test: trace");
112     hts_log_debug(__FUNCTION__, "Test: debug");
113     hts_log_info(__FUNCTION__,  "Test: info");
114     hts_log_warning(__FUNCTION__,"Test: warning");
115     hts_log_error(__FUNCTION__, "Test: error");
116 }