omdl  v0.9.5
OpenSCAD Mechanical Design Library
console.scad
Go to the documentation of this file.
1 //! Message logging functions.
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 2015-2024
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \amu_define group_name (Console Output)
31  \amu_define group_brief (Console message logging.)
32 
33  \amu_include (include/amu/pgid_path_pstem_pg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/group_in_parent_start.amu)
42  \amu_include (include/amu/includes_required.amu)
43 
44  \details
45 
46  \amu_define title (Console)
47  \amu_include (include/amu/scope.amu)
48 *******************************************************************************/
49 
50 //----------------------------------------------------------------------------//
51 
52 //! Format the function call stack as a string.
53 /***************************************************************************//**
54  \param b <integer> The stack index bottom offset.
55  Return function names above this offset.
56  \param t <integer> The stack index top offset.
57  Return function names below this offset.
58 
59  \returns <string> A string-formatted colon-separated list of
60  functions names for the current function call stack.
61 
62  \note Returns \b undef when \p b is greater than the current number
63  of function instances (ie: <tt>b > $parent_modules-1</tt>).
64  \note Returns the string \c "root()" when the function call stack
65  is empty (ie: at the root of a script).
66 *******************************************************************************/
67 function stack
68 (
69  b = 0,
70  t = 0
71 ) = let
72  (
73  bo = abs(b),
74  to = abs(t),
75  i = $parent_modules - 1 - bo
76  )
77  ($parent_modules == undef) ? "root()"
78  : (bo > $parent_modules-1) ? undef
79  : (i < to) ? "root()"
80  : (i == to) ? str( parent_module( i ), "()" )
81  : str( parent_module( i ), "(): ", stack( bo + 1, to ) );
82 
83 //! Output message to console.
84 /***************************************************************************//**
85  \param m <string> An output message.
86 *******************************************************************************/
87 module log_echo( m )
88 {
89  um = (m==undef) ? "" : m;
90 
91  echo ( um );
92 }
93 
94 //! Output diagnostic message to console.
95 /***************************************************************************//**
96  \param t <string> An output message type.
97  \param m <string> An output message.
98 *******************************************************************************/
99 module log_type( t, m )
100 {
101  um = (m==undef) ? "" : m;
102  mt = (t==undef) ? "NO_TYPE" : t;
103 
104  sp = chr( 32 );
105 
106  echo ( str("[", sp, mt, sp, "]", sp, um) );
107 }
108 
109 //! Output diagnostic message to console.
110 /***************************************************************************//**
111  \param m <string> An output message.
112 
113  \details
114 
115  Message is written if and only if \p $log_debug is \p true.
116 *******************************************************************************/
117 module log_debug( m )
118 {
119  um = (m==undef) ? "" : m;
120 
121  mt = "[ DEBUG ]";
122  sp = chr( 32 );
123  cs = stack( t = 1 );
124 
125  if ( $log_debug == true )
126  echo ( str(mt, sp, cs, ";", sp, um) );
127 }
128 
129 //! Output information message to console.
130 /***************************************************************************//**
131  \param m <string> An output message.
132 *******************************************************************************/
133 module log_info( m )
134 {
135  um = (m==undef) ? "" : m;
136 
137  mt = "[ INFO ]";
138  sp = chr( 32 );
139  cs = stack( t = 1 );
140 
141  echo ( str(mt, sp, cs, ";", sp, um) );
142 }
143 
144 //! Output warning message to console.
145 /***************************************************************************//**
146  \param m <string> An output message.
147 *******************************************************************************/
148 module log_warn( m )
149 {
150  um = (m==undef) ? "" : m;
151 
152  mt = "[ WARNING ]";
153  bc = 35;
154  sp = chr( 32 );
155  cs = stack( t = 1 );
156  hb = chr( [for (i=[0:1:len(um)+len(mt)+4]) bc] );
157  ms = str(chr(bc), sp, mt, sp, um, sp, chr(bc));
158 
159  echo ();
160  echo (cs);
161  echo (hb);
162  echo (ms);
163  echo (hb);
164 }
165 
166 //! Output error message to console.
167 /***************************************************************************//**
168  \param m <string> An output message.
169 
170  \details
171 
172  Output an error message to the console.
173 
174  \note
175  Starting with version 2019.05, the [assert] functions which can be
176  used to generate an error condition and halt script processing.
177 
178  [assert]: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#assert
179 *******************************************************************************/
180 module log_error( m )
181 {
182  um = (m==undef) ? "" : m;
183 
184  mt = "[ ERROR ]";
185  bc = 35;
186  sp = chr( 32 );
187  cs = stack( t = 1 );
188  hb = chr( [for (i=[0:1:len(um)+len(mt)+6]) bc] );
189  hs = chr( concat([bc, bc],[for (i=[0:1:len(um)+len(mt)+2]) 32],[bc, bc]) );
190  ms = str(chr(bc), chr(bc), sp, mt, sp, um, sp, chr(bc), chr(bc));
191 
192  echo ();
193  echo (cs);
194  echo (hb); echo (hb);
195  echo (hs);
196  echo (ms);
197  echo (hs);
198  echo (hb); echo (hb);
199 
200  color( "red" )
201  text( ms );
202 }
203 
204 //! @}
205 //! @}
206 
207 //----------------------------------------------------------------------------//
208 // openscad-amu auxiliary scripts
209 //----------------------------------------------------------------------------//
210 
211 /*
212 BEGIN_SCOPE example;
213  BEGIN_OPENSCAD;
214  include <omdl-base.scad>;
215 
216  $log_debug = true;
217  message = "console log message";
218 
219  // general
220  log_echo( message );
221 
222  // type
223  log_type( "MY_TYPE", message );
224 
225  // debugging
226  log_debug( message );
227  log_debug( message, $log_debug = false );
228 
229  // information
230  log_info( message );
231 
232  // warning
233  log_warn( message );
234 
235  // error
236  log_error( message );
237 
238  // end_include
239  END_OPENSCAD;
240 
241  BEGIN_MFSCRIPT;
242  include --path "${INCLUDE_PATH}" {var_init,var_gen_term}.mfs;
243  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
244  END_MFSCRIPT;
245 END_SCOPE;
246 */
247 
248 //----------------------------------------------------------------------------//
249 // end of file
250 //----------------------------------------------------------------------------//
module log_debug(m)
Output diagnostic message to console.
Definition: console.scad:302
module log_warn(m)
Output warning message to console.
Definition: console.scad:333
module log_error(m)
Output error message to console.
Definition: console.scad:365
module log_echo(m)
Output message to console.
Definition: console.scad:272
function stack(b=0, t=0)
Format the function call stack as a string.
module log_type(t, m)
Output diagnostic message to console.
Definition: console.scad:284
module log_info(m)
Output information message to console.
Definition: console.scad:318