omdl  v0.6.1
OpenSCAD Mechanical Design Library
console.scad
Go to the documentation of this file.
1 //! Message logging functions.
2 /***************************************************************************//**
3  \file console.scad
4  \author Roy Allen Sutton
5  \date 2015-2017
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  \ingroup utilities utilities_console
31 *******************************************************************************/
32 
33 //----------------------------------------------------------------------------//
34 /***************************************************************************//**
35  \addtogroup utilities
36  @{
37 
38  \defgroup utilities_console Console
39  \brief Console message logging.
40 
41  \details
42 
43  \b Example
44 
45  \dontinclude console_example.scad
46  \skip use
47  \until log_error( message );
48 
49  \b Result \include console_example.log
50 
51  @{
52 *******************************************************************************/
53 //----------------------------------------------------------------------------//
54 
55 //! Format the function call stack as a string.
56 /***************************************************************************//**
57  \param b <integer> The stack index bottom offset.
58  Return function names above this offset.
59  \param t <integer> The stack index top offset.
60  Return function names below this offset.
61 
62  \returns <string> A string-formatted colon-separated list of
63  functions names for the current function call stack.
64 
65  \note Returns \b undef when \p b is greater than the current number
66  of function instances (ie: <tt>b > $parent_modules-1</tt>).
67  \note Returns the string \c "root()" when the function call stack
68  is empty (ie: at the root of a script).
69 *******************************************************************************/
70 function stack
71 (
72  b = 0,
73  t = 0
74 ) = let
75  (
76  bo = abs(b),
77  to = abs(t),
78  i = $parent_modules - 1 - bo
79  )
80  ($parent_modules == undef) ? "root()"
81  : (bo > $parent_modules-1) ? undef
82  : (i < to) ? "root()"
83  : (i == to) ? str( parent_module( i ), "()" )
84  : str( parent_module( i ), "(): ", stack( bo + 1, to ) );
85 
86 //! Output message to console.
87 /***************************************************************************//**
88  \param m <string> An output message.
89 *******************************************************************************/
90 module log_echo( m )
91 {
92  um = (m==undef) ? "" : m;
93 
94  echo ( um );
95 }
96 
97 //! Output diagnostic message to console.
98 /***************************************************************************//**
99  \param m <string> An output message.
100 
101  \details
102 
103  Message is written if and only if \p $log_debug is \p true.
104 *******************************************************************************/
105 module log_debug( m )
106 {
107  um = (m==undef) ? "" : m;
108 
109  mt = "[ DEBUG ]";
110  sp = chr( 32 );
111  cs = stack( t = 1 );
112 
113  if ( $log_debug == true )
114  echo ( str(mt, sp, cs, ";", sp, um) );
115 
116 }
117 
118 //! Output information message to console.
119 /***************************************************************************//**
120  \param m <string> An output message.
121 *******************************************************************************/
122 module log_info( m )
123 {
124  um = (m==undef) ? "" : m;
125 
126  mt = "[ INFO ]";
127  sp = chr( 32 );
128  cs = stack( t = 1 );
129 
130  echo ( str(mt, sp, cs, ";", sp, um) );
131 }
132 
133 //! Output warning message to console.
134 /***************************************************************************//**
135  \param m <string> An output message.
136 *******************************************************************************/
137 module log_warn( m )
138 {
139  um = (m==undef) ? "" : m;
140 
141  mt = "[ WARNING ]";
142  bc = 35;
143  sp = chr( 32 );
144  cs = stack( t = 1 );
145  hb = chr( [for (i=[0:1:len(um)+len(mt)+4]) bc] );
146  ms = str(chr(bc), sp, mt, sp, um, sp, chr(bc));
147 
148  echo ();
149  echo (cs);
150  echo (hb);
151  echo (ms);
152  echo (hb);
153 }
154 
155 //! Output error message to console.
156 /***************************************************************************//**
157  \param m <string> An output message.
158 
159  \details
160 
161  Output an error message to the console. Ideally, rendering should halt
162  and the script should exit. However, no suitable abort function exists.
163  To alert of the critical error, the error message is also rendered
164  graphically.
165 *******************************************************************************/
166 module log_error( m )
167 {
168  um = (m==undef) ? "" : m;
169 
170  mt = "[ ERROR ]";
171  bc = 35;
172  sp = chr( 32 );
173  cs = stack( t = 1 );
174  hb = chr( [for (i=[0:1:len(um)+len(mt)+6]) bc] );
175  hs = chr( concat([bc, bc],[for (i=[0:1:len(um)+len(mt)+2]) 32],[bc, bc]) );
176  ms = str(chr(bc), chr(bc), sp, mt, sp, um, sp, chr(bc), chr(bc));
177 
178  echo ();
179  echo (cs);
180  echo (hb); echo (hb);
181  echo (hs);
182  echo (ms);
183  echo (hs);
184  echo (hb); echo (hb);
185 
186  color( "red" )
187  text( ms );
188 }
189 
190 //! @}
191 //! @}
192 
193 //----------------------------------------------------------------------------//
194 // openscad-amu auxiliary scripts
195 //----------------------------------------------------------------------------//
196 
197 /*
198 BEGIN_SCOPE example;
199  BEGIN_OPENSCAD;
200  include <console.scad>;
201 
202  $log_debug = true;
203  message = "console log message";
204 
205  // general
206  log_echo( message );
207 
208  // debugging
209  log_debug( message );
210  log_debug( message, $log_debug = false );
211 
212  // information
213  log_info( message );
214 
215  // warning
216  log_warn( message );
217 
218  // error
219  log_error( message );
220  END_OPENSCAD;
221 
222  BEGIN_MFSCRIPT;
223  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
224  include --path "${INCLUDE_PATH}" script_std.mfs;
225  END_MFSCRIPT;
226 END_SCOPE;
227 */
228 
229 //----------------------------------------------------------------------------//
230 // end of file
231 //----------------------------------------------------------------------------//
module log_echo(m)
Output message to console.
Definition: console.scad:90
function stack(b=0, t=0)
Format the function call stack as a string.
module log_warn(m)
Output warning message to console.
Definition: console.scad:137
module log_info(m)
Output information message to console.
Definition: console.scad:122
module log_error(m)
Output error message to console.
Definition: console.scad:166
module log_debug(m)
Output diagnostic message to console.
Definition: console.scad:105