omdl  v0.5
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 data data_console
31 *******************************************************************************/
32 
33 use <utilities.scad>;
34 
35 //----------------------------------------------------------------------------//
36 /***************************************************************************//**
37  \addtogroup data
38  @{
39 
40  \defgroup data_console Console
41  \brief Console message logging.
42 
43  \details
44 
45  \b Example
46 
47  \dontinclude console_example.scad
48  \skip use
49  \until log_error( message );
50 
51  \b Result \include console_example.log
52 
53  @{
54 *******************************************************************************/
55 //----------------------------------------------------------------------------//
56 
57 //! Output message to console.
58 /***************************************************************************//**
59  \param m <string> An output message.
60 *******************************************************************************/
61 module log_echo( m )
62 {
63  um = (m==undef) ? "" : m;
64 
65  echo ( um );
66 }
67 
68 //! Output diagnostic message to console.
69 /***************************************************************************//**
70  \param m <string> An output message.
71 
72  \details
73 
74  When \p $log_debug == \p true, message is written to the console. When
75  \p false, output is not generated.
76 *******************************************************************************/
77 module log_debug( m )
78 {
79  um = (m==undef) ? "" : m;
80 
81  mt = "[ DEBUG ]";
82  sp = chr( 32 );
83  cs = stack( t = 1 );
84 
85  if ( $log_debug == true )
86  echo ( str(mt, sp, cs, ";", sp, um) );
87 
88 }
89 
90 //! Output information message to console.
91 /***************************************************************************//**
92  \param m <string> An output message.
93 *******************************************************************************/
94 module log_info( m )
95 {
96  um = (m==undef) ? "" : m;
97 
98  mt = "[ INFO ]";
99  sp = chr( 32 );
100  cs = stack( t = 1 );
101 
102  echo ( str(mt, sp, cs, ";", sp, um) );
103 }
104 
105 //! Output warning message to console.
106 /***************************************************************************//**
107  \param m <string> An output message.
108 *******************************************************************************/
109 module log_warn( m )
110 {
111  um = (m==undef) ? "" : m;
112 
113  mt = "[ WARNING ]";
114  bc = 35;
115  sp = chr( 32 );
116  cs = stack( t = 1 );
117  hb = chr( [for (i=[0:1:len(um)+len(mt)+4]) bc] );
118  ms = str(chr(bc), sp, mt, sp, um, sp, chr(bc));
119 
120  echo ();
121  echo (cs);
122  echo (hb);
123  echo (ms);
124  echo (hb);
125 }
126 
127 //! Output error message to console.
128 /***************************************************************************//**
129  \param m <string> An output message.
130 
131  \details
132 
133  Output an error message to the console. Ideally, rendering should halt
134  and the script should exit. However, no suitable abort function exists.
135  To alert of the critical error, the error message is also rendered
136  graphically.
137 *******************************************************************************/
138 module log_error( m )
139 {
140  um = (m==undef) ? "" : m;
141 
142  mt = "[ ERROR ]";
143  bc = 35;
144  sp = chr( 32 );
145  cs = stack( t = 1 );
146  hb = chr( [for (i=[0:1:len(um)+len(mt)+6]) bc] );
147  hs = chr( concat([bc, bc],[for (i=[0:1:len(um)+len(mt)+2]) 32],[bc, bc]) );
148  ms = str(chr(bc), chr(bc), sp, mt, sp, um, sp, chr(bc), chr(bc));
149 
150  echo ();
151  echo (cs);
152  echo (hb); echo (hb);
153  echo (hs);
154  echo (ms);
155  echo (hs);
156  echo (hb); echo (hb);
157 
158  color( "red" )
159  text( ms );
160 }
161 
162 //! @}
163 //! @}
164 
165 //----------------------------------------------------------------------------//
166 // openscad-amu auxiliary scripts
167 //----------------------------------------------------------------------------//
168 
169 /*
170 BEGIN_SCOPE example;
171  BEGIN_OPENSCAD;
172  use <console.scad>;
173 
174  $log_debug = true;
175  message = "console log message";
176 
177  // general
178  log_echo( message );
179 
180  // debugging
181  log_debug( message );
182  log_debug( message, $log_debug = false );
183 
184  // information
185  log_info( message );
186 
187  // warning
188  log_warn( message );
189 
190  // error
191  log_error( message );
192  END_OPENSCAD;
193 
194  BEGIN_MFSCRIPT;
195  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
196  include --path "${INCLUDE_PATH}" script_std.mfs;
197  END_MFSCRIPT;
198 END_SCOPE;
199 */
200 
201 //----------------------------------------------------------------------------//
202 // end of file
203 //----------------------------------------------------------------------------//
module log_error(m)
Output error message to console.
Definition: console.scad:138
function stack(b=0, t=0)
Format the function call stack as a string.
module log_info(m)
Output information message to console.
Definition: console.scad:94
module log_echo(m)
Output message to console.
Definition: console.scad:61
module log_debug(m)
Output diagnostic message to console.
Definition: console.scad:77
module log_warn(m)
Output warning message to console.
Definition: console.scad:109