Skip to content

Commit b95d440

Browse files
authored
[logging] refactor logging (#247)
* refactor logging Signed-off-by: Anton Dukhovnikov <antond@wetafx.co.nz> * address review comments Signed-off-by: Anton Dukhovnikov <antond@wetafx.co.nz> * fix formatting Signed-off-by: Anton Dukhovnikov <antond@wetafx.co.nz> * fix unused Signed-off-by: Anton Dukhovnikov <antond@wetafx.co.nz> --------- Signed-off-by: Anton Dukhovnikov <antond@wetafx.co.nz>
1 parent 62190f8 commit b95d440

15 files changed

Lines changed: 281 additions & 213 deletions

include/rawtoaces/rawtoaces_core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class SpectralSolver
169169
/// @pre white balance calculation must have been performed successfully
170170
const std::vector<double> &get_WB_multipliers() const;
171171

172+
/// Error message from the most recent method call that returned false.
173+
std::string last_error_message;
174+
172175
int verbosity = 0;
173176

174177
private:

include/rawtoaces/spectral_data.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,17 @@ struct SpectralData
128128
/// The spectral data storage.
129129
std::map<std::string, SpectralSet> data;
130130

131-
bool load( const std::string &path, bool reshape = true );
131+
/// Loads spectral data from a given file path.
132+
/// @param path a path to the file to load data from
133+
/// @param reshape if set to `true`, the data will be reshaped to the
134+
/// reference shape (`rta::core::Spectrum::ReferenceShape`).
135+
/// @param error_message an optional destination for any error
136+
/// message occured during loading.
137+
/// @result `true` if loaded successfully.
138+
bool load(
139+
const std::string &path,
140+
bool reshape = true,
141+
std::string *error_message = nullptr );
132142

133143
/// A convenience operator returning the `Spectrum` of a given channel name
134144
/// in the "main" data set.

src/rawtoaces_core/rawtoaces_core.cpp

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ void calculate_daylight_SPD( const int &cct_input, Spectrum &spectrum )
5454
else if ( cct_input >= 4000 && cct_input <= 25000 )
5555
cct = cct_input * 1.0;
5656
else
57-
{
58-
std::cerr << "The range of Correlated Color Temperature for "
59-
<< "Day Light should be from 4000 to 25000." << std::endl;
60-
exit( 1 );
61-
}
57+
assert( false );
6258

6359
spectrum.values.clear();
6460

@@ -100,12 +96,7 @@ void calculate_daylight_SPD( const int &cct_input, Spectrum &spectrum )
10096

10197
void calculate_blackbody_SPD( const int &cct, Spectrum &spectrum )
10298
{
103-
if ( cct < 1500 || cct >= 4000 )
104-
{
105-
std::cerr << "The range of Color Temperature for BlackBody "
106-
<< "should be from 1500 to 3999." << std::endl;
107-
exit( 1 );
108-
}
99+
assert( cct >= 1500 && cct < 4000 );
109100

110101
spectrum.values.clear();
111102

@@ -129,12 +120,15 @@ void calculate_blackbody_SPD( const int &cct, Spectrum &spectrum )
129120
/// @param type Type of light source (e.g. "d50", "d65", "d75", "A", "B", "C", "D50", "D65", "D75")
130121
/// @param is_daylight True if the light source is a daylight source, false if it is a blackbody source
131122
/// @param illuminant Reference to SpectralData object to fill with generated illuminant data
123+
/// @param error_message a destination for any potential error message.
124+
/// @result `true` if generated successfully.
132125
/// @pre cct is in valid range for the specified illuminant type
133-
void generate_illuminant(
126+
bool generate_illuminant(
134127
int cct,
135128
const std::string &type,
136129
bool is_daylight,
137-
SpectralData &illuminant )
130+
SpectralData &illuminant,
131+
std::string &error_message )
138132
{
139133
illuminant.data.clear();
140134

@@ -150,12 +144,30 @@ void generate_illuminant(
150144
illuminant.type = type;
151145
if ( is_daylight )
152146
{
147+
if ( cct < 40 || ( cct > 250 && cct < 4000 ) || cct > 25000 )
148+
{
149+
error_message =
150+
"The range of Correlated Color Temperature for "
151+
"Day Light should be from 4000 to 25000.\n";
152+
return false;
153+
}
154+
153155
calculate_daylight_SPD( cct, power_spectrum );
154156
}
155157
else
156158
{
159+
if ( cct < 1500 || cct >= 4000 )
160+
{
161+
error_message =
162+
"The range of Color Temperature for BlackBody "
163+
"should be from 1500 to 3999.\n";
164+
return false;
165+
}
166+
157167
calculate_blackbody_SPD( cct, power_spectrum );
158168
}
169+
170+
return true;
159171
}
160172

161173
SpectralSolver::SpectralSolver(
@@ -328,15 +340,15 @@ bool SpectralSolver::find_illuminant( const std::string &type )
328340
{
329341
int cct = atoi( type.substr( 1 ).c_str() );
330342
const std::string illuminant_type = "d" + std::to_string( cct );
331-
generate_illuminant( cct, illuminant_type, true, illuminant );
332-
return true;
343+
return generate_illuminant(
344+
cct, illuminant_type, true, illuminant, last_error_message );
333345
}
334346
else if ( is_blackbody )
335347
{
336348
int cct = atoi( type.substr( 0, type.length() - 1 ).c_str() );
337349
const std::string illuminant_type = std::to_string( cct ) + "k";
338-
generate_illuminant( cct, illuminant_type, false, illuminant );
339-
return true;
350+
return generate_illuminant(
351+
cct, illuminant_type, false, illuminant, last_error_message );
340352
}
341353
else
342354
{
@@ -360,8 +372,9 @@ bool SpectralSolver::find_illuminant( const vector<double> &wb )
360372
if ( camera.data.count( "main" ) == 0 ||
361373
camera.data.at( "main" ).size() != 3 )
362374
{
363-
std::cerr << "ERROR: camera needs to be initialised prior to calling "
364-
<< "SpectralSolver::find_illuminant()" << std::endl;
375+
last_error_message =
376+
"Camera needs to be initialised prior to calling "
377+
"SpectralSolver::find_illuminant().";
365378
return false;
366379
}
367380

@@ -372,15 +385,21 @@ bool SpectralSolver::find_illuminant( const vector<double> &wb )
372385
{
373386
SpectralData &illuminant_data = _all_illuminants.emplace_back();
374387
const std::string type = "d" + std::to_string( cct / 100 );
375-
generate_illuminant( cct, type, true, illuminant_data );
388+
389+
[[maybe_unused]] bool success = generate_illuminant(
390+
cct, type, true, illuminant_data, last_error_message );
391+
assert( success );
376392
}
377393

378394
// Blackbody - pre-calculate
379395
for ( int cct = 1500; cct < 4000; cct += 500 )
380396
{
381397
SpectralData &illuminant_data = _all_illuminants.emplace_back();
382398
const std::string type = std::to_string( cct ) + "k";
383-
generate_illuminant( cct, type, false, illuminant_data );
399+
400+
[[maybe_unused]] bool success = generate_illuminant(
401+
cct, type, false, illuminant_data, last_error_message );
402+
assert( success );
384403
}
385404

386405
auto illuminant_files = collect_data_files( "illuminant" );
@@ -425,16 +444,18 @@ bool SpectralSolver::calculate_WB()
425444
if ( camera.data.count( "main" ) == 0 ||
426445
camera.data.at( "main" ).size() != 3 )
427446
{
428-
std::cerr << "ERROR: camera needs to be initialised prior to calling "
429-
<< "SpectralSolver::calculate_WB()" << std::endl;
447+
last_error_message =
448+
"Camera needs to be initialised prior to calling "
449+
"SpectralSolver::calculate_WB().";
430450
return false;
431451
}
432452

433453
if ( illuminant.data.count( "main" ) == 0 ||
434454
illuminant.data.at( "main" ).size() != 1 )
435455
{
436-
std::cerr << "ERROR: illuminant needs to be initialised prior to "
437-
<< "calling SpectralSolver::calculate_WB()" << std::endl;
456+
last_error_message =
457+
"Illuminant needs to be initialised prior to "
458+
"calling SpectralSolver::calculate_WB().";
438459
return false;
439460
}
440461

@@ -717,34 +738,36 @@ bool SpectralSolver::calculate_IDT_matrix()
717738
if ( camera.data.count( "main" ) == 0 ||
718739
camera.data.at( "main" ).size() != 3 )
719740
{
720-
std::cerr << "ERROR: camera needs to be initialised prior to calling "
721-
<< "SpectralSolver::calculate_IDT_matrix()" << std::endl;
741+
last_error_message =
742+
"Camera needs to be initialised prior to calling "
743+
"SpectralSolver::calculate_IDT_matrix().";
722744
return false;
723745
}
724746

725747
if ( illuminant.data.count( "main" ) == 0 ||
726748
illuminant.data.at( "main" ).size() != 1 )
727749
{
728-
std::cerr << "ERROR: illuminant needs to be initialised prior to "
729-
<< "calling SpectralSolver::calculate_IDT_matrix()"
730-
<< std::endl;
750+
last_error_message =
751+
"Illuminant needs to be initialised prior to "
752+
"calling SpectralSolver::calculate_IDT_matrix().";
731753
return false;
732754
}
733755

734756
if ( observer.data.count( "main" ) == 0 ||
735757
observer.data.at( "main" ).size() != 3 )
736758
{
737-
std::cerr << "ERROR: observer needs to be initialised prior to calling "
738-
<< "SpectralSolver::calculate_IDT_matrix()" << std::endl;
759+
last_error_message =
760+
"Observer needs to be initialised prior to "
761+
"calling SpectralSolver::calculate_IDT_matrix().";
739762
return false;
740763
}
741764

742765
if ( training_data.data.count( "main" ) == 0 ||
743766
training_data.data.at( "main" ).empty() )
744767
{
745-
std::cerr << "ERROR: training data needs to be initialised prior to "
746-
<< "calling SpectralSolver::calculate_IDT_matrix()"
747-
<< std::endl;
768+
last_error_message =
769+
"Training data needs to be initialised prior to "
770+
"calling SpectralSolver::calculate_IDT_matrix().";
748771
return false;
749772
}
750773

src/rawtoaces_core/spectral_data.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <assert.h>
77
#include <fstream>
88
#include <iostream>
9+
#include <sstream>
910
#include <nlohmann/json.hpp>
1011

1112
namespace rta
@@ -178,7 +179,8 @@ parse_string( nlohmann::json &j, std::string &dst, const std::string &key )
178179
dst = v;
179180
}
180181

181-
bool SpectralData::load( const std::string &path, bool reshape )
182+
bool SpectralData::load(
183+
const std::string &path, bool reshape, std::string *error_message )
182184
{
183185
// Reset all in case the object has been initialised before.
184186
manufacturer.erase();
@@ -206,8 +208,10 @@ bool SpectralData::load( const std::string &path, bool reshape )
206208
std::ifstream i( path );
207209
if ( !i.is_open() )
208210
{
209-
std::cerr << "Error: Failed to open file " << path << "."
210-
<< std::endl;
211+
if ( error_message != nullptr )
212+
{
213+
*error_message = "Failed to open file " + path + ".";
214+
}
211215
return false;
212216
}
213217
nlohmann::json file_data = nlohmann::json::parse( i );
@@ -282,10 +286,15 @@ bool SpectralData::load( const std::string &path, bool reshape )
282286

283287
if ( shape.step != 0 && new_step != shape.step )
284288
{
285-
std::cerr << "Error: Inconsistent wavelength step "
286-
<< "detected in " << path
287-
<< ". Expected: " << shape.step
288-
<< ", got: " << new_step << "." << std::endl;
289+
if ( error_message != nullptr )
290+
{
291+
std::stringstream stream;
292+
stream << "Inconsistent wavelength "
293+
<< "step detected in " << path
294+
<< ". Expected: " << shape.step
295+
<< ", got: " << new_step << ".";
296+
*error_message = stream.str();
297+
}
289298
return false;
290299
}
291300

@@ -321,14 +330,20 @@ bool SpectralData::load( const std::string &path, bool reshape )
321330
}
322331
catch ( nlohmann::detail::parse_error &error )
323332
{
324-
std::cerr << "Error: JSON parsing of " << path
325-
<< " failed with error: " << error.what() << std::endl;
333+
if ( error_message != nullptr )
334+
{
335+
*error_message = "Error: JSON parsing of " + path +
336+
" failed with error: " + error.what() + ".";
337+
}
326338
return false;
327339
}
328340
catch ( const std::exception &error )
329341
{
330-
std::cerr << "Error: JSON parsing of " << path
331-
<< " failed with error: " << error.what() << std::endl;
342+
if ( error_message != nullptr )
343+
{
344+
*error_message = "Error: JSON parsing of " + path +
345+
" failed with error: " + error.what() + ".";
346+
}
332347
return false;
333348
}
334349

src/rawtoaces_util/colour_transforms.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void print_data_error(
2929
}
3030
error_message +=
3131
". Please check the database search path in "
32-
"RAWTOACES_DATABASE_PATH";
32+
"RAWTOACES_DATABASE_PATH.";
3333
}
3434

3535
bool configure_spectral_solver(
@@ -116,7 +116,7 @@ bool solve_illuminant_from_multipliers(
116116
if ( !solver.find_illuminant( wb_multipliers ) )
117117
{
118118
error_message =
119-
"Failed to find illuminant from white balance multipliers";
119+
"Failed to find illuminant from white balance multipliers.";
120120
return false;
121121
}
122122

@@ -126,6 +126,7 @@ bool solve_illuminant_from_multipliers(
126126
cache_data.second[1] = multipliers[1];
127127
cache_data.second[2] = multipliers[2];
128128

129+
error_message = "";
129130
return true;
130131
}
131132

@@ -204,7 +205,8 @@ bool solve_multipliers_from_illuminant(
204205

205206
if ( !solver.calculate_WB() )
206207
{
207-
error_message = "Failed to calculate white balance multipliers";
208+
error_message = "Failed to calculate white balance multipliers. " +
209+
solver.last_error_message;
208210
return false;
209211
}
210212

@@ -213,6 +215,7 @@ bool solve_multipliers_from_illuminant(
213215
cache_data[1] = multipliers[1];
214216
cache_data[2] = multipliers[2];
215217

218+
error_message = "";
216219
return true;
217220
}
218221

@@ -299,11 +302,13 @@ bool solve_matrix_from_illuminant(
299302

300303
if ( !solver.calculate_WB() )
301304
{
305+
error_message = solver.last_error_message;
302306
return false;
303307
}
304308

305309
if ( !solver.calculate_IDT_matrix() )
306310
{
311+
error_message = solver.last_error_message;
307312
return false;
308313
}
309314

@@ -348,11 +353,8 @@ bool fetch_matrix_from_illuminant(
348353
bool success = entry.first;
349354
if ( !success )
350355
{
351-
// Set fallback error message if none was set by solve_matrix_from_illuminant
352-
if ( error_message.empty() )
353-
{
354-
error_message = "Failed to calculate IDT matrix from illuminant";
355-
}
356+
error_message =
357+
"Failed to calculate IDT matrix from illuminant. " + error_message;
356358
return false;
357359
}
358360

0 commit comments

Comments
 (0)