Skip to content

Commit d0c1515

Browse files
committed
modifications to prepare for a proper error management. Changed the way OpenJPH version is created, and modified CMakeLists.txt to remove -g and add -DNDEBUG
1 parent ccc2d83 commit d0c1515

File tree

8 files changed

+89
-16
lines changed

8 files changed

+89
-16
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project (openjph VERSION 0.1.0 DESCRIPTION "Open source implementation of JPH" L
55
if (MSVC)
66
set(CMAKE_CXX_FLAGS "/EHsc")
77
else()
8-
set(CMAKE_CXX_FLAGS "-std=c++11 -O3 -g")
8+
set(CMAKE_CXX_FLAGS "-std=c++11 -O3 -DNDEBUG")
99
endif()
1010

1111
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)

src/apps/ojph_compress/ojph_compress.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "ojph_file.h"
4646
#include "ojph_codestream.h"
4747
#include "ojph_params.h"
48+
#include "ojph_message.h"
4849

4950
/////////////////////////////////////////////////////////////////////////////
5051
struct size_list_interpreter : public ojph::cli_interpreter::arg_inter_base
@@ -728,6 +729,13 @@ int main(int argc, char * argv[]) {
728729
printf("%s\n", e);
729730
exit (-1);
730731
}
732+
catch (const std::exception& e)
733+
{
734+
const char *p = e.what();
735+
if (strncmp(p, "ojph error", 10) != 0)
736+
printf("%s\n", p);
737+
exit(-1);
738+
}
731739

732740
clock_t end = clock();
733741
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

src/core/codestream/ojph_codestream.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,10 @@ namespace ojph {
376376
if (!qcd.write(file))
377377
throw "Error writing to file";
378378

379-
char buf[] = " OpenJPH Ver 0.1.1";
379+
char buf[] = " OpenJPH Ver "
380+
OJPH_INT_TO_STRING(OJPH_CORE_VER_MAJOR) "."
381+
OJPH_INT_TO_STRING(OJPH_CORE_VER_MINOR) "."
382+
OJPH_INT_TO_STRING(OJPH_CORE_VER_SUBMINOR) ".";
380383
size_t len = strlen(buf);
381384
*(ui16*)buf = swap_byte(JP2K_MARKER::COM);
382385
*(ui16*)(buf + 2) = swap_byte((ui16)(len - 2));

src/core/common/ojph_arch.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,21 @@ namespace ojph {
154154
return reinterpret_cast<T *>(p);
155155
}
156156

157+
////////////////////////////////////////////////////////////////////////////
158+
// OS detection definitions
159+
////////////////////////////////////////////////////////////////////////////
160+
#if (defined WIN32) || (defined _WIN32) || (defined _WIN64)
161+
#define OJPH_OS_WINDOWS
162+
#elif (defined __APPLE__)
163+
#define OJPH_OS_APPLE
164+
#elif (defined __linux)
165+
#define OJPH_OS_LINUX
166+
#endif
167+
157168
/////////////////////////////////////////////////////////////////////////////
158169
// defines for dll
159170
/////////////////////////////////////////////////////////////////////////////
160-
#ifdef OJPH_COMPILER_MSVC
171+
#ifdef OJPH_OS_WINDOWS
161172
#define OJPH_EXPORT __declspec(dllexport)
162173
#else
163174
#define OJPH_EXPORT

src/core/common/ojph_defs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ typedef int32_t si32;
5555
typedef uint64_t ui64;
5656
typedef int64_t si64;
5757

58+
/////////////////////////////////////////////////////////////////////////////
59+
#define OJPH_CORE_VER_MAJOR 0
60+
#define OJPH_CORE_VER_MINOR 2
61+
#define OJPH_CORE_VER_SUBMINOR 0
62+
63+
/////////////////////////////////////////////////////////////////////////////
64+
#define OJPH_INT_TO_STRING(I) #I
65+
5866
/////////////////////////////////////////////////////////////////////////////
5967
// number of fractional bits for 16 bit representation
6068
// for 32 bits, it is NUM_FRAC_BITS + 16

src/core/common/ojph_message.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,41 @@
4040

4141
namespace ojph {
4242

43-
///////////////////////////////////////////////////////////////////////////
43+
////////////////////////////////////////////////////////////////////////////
4444
void set_error_stream(FILE *s);
4545

46-
///////////////////////////////////////////////////////////////////////////
47-
void set_output_stream(FILE *s);
46+
////////////////////////////////////////////////////////////////////////////
47+
void set_warning_stream(FILE *s);
4848

49-
///////////////////////////////////////////////////////////////////////////
50-
void error(int error_code, const char* file_name, int line_num);
49+
////////////////////////////////////////////////////////////////////////////
50+
void warn(int warn_code, const char* file_name, int line_num,
51+
const char *fmt, ...)
52+
#ifdef OJPH_COMPILER_GNUC
53+
__attribute__((format(printf, 4, 5)))
54+
#endif
55+
;
5156

57+
////////////////////////////////////////////////////////////////////////////
58+
void error(int error_code, const char* file_name, int line_num,
59+
const char *fmt, ...)
60+
#ifdef OJPH_COMPILER_GNUC
61+
__attribute__((format(printf, 4, 5)))
62+
#endif
63+
;
5264
}
5365

66+
//////////////////////////////////////////////////////////////////////////////
67+
#if (defined OJPH_OS_WINDOWS)
68+
#define __OJPHFILE__ \
69+
(strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
70+
#else
71+
#define __OJPHFILE__ \
72+
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
73+
#endif
74+
//////////////////////////////////////////////////////////////////////////////
75+
#define OJPH_WARN(t, ...) ojph::warn(t, __OJPHFILE__, __LINE__, __VA_ARGS__);
76+
//////////////////////////////////////////////////////////////////////////////
77+
#define OJPH_ERROR(t, ...) ojph::error(t, __OJPHFILE__, __LINE__,__VA_ARGS__);
78+
79+
5480
#endif // !OJPH_MESSAGE_H

src/core/others/ojph_message.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636
/****************************************************************************/
3737

3838
#include <cstdio>
39+
#include <cstdarg>
40+
#include <stdexcept>
3941

4042
#include "ojph_message.h"
4143

4244
namespace ojph {
4345

4446
////////////////////////////////////////////////////////////////////////////
45-
FILE *output_stream = stdout;
47+
FILE *warning_stream = stdout;
4648
FILE *error_stream = stderr;
4749

4850
////////////////////////////////////////////////////////////////////////////
@@ -52,21 +54,36 @@ namespace ojph {
5254
}
5355

5456
////////////////////////////////////////////////////////////////////////////
55-
void set_output_stream(FILE *s)
57+
void set_warning_stream(FILE *s)
5658
{
57-
output_stream = s;
59+
warning_stream = s;
5860
}
5961

6062
////////////////////////////////////////////////////////////////////////////
61-
void error(int error_code, const char* file_name, int line_num)
63+
void error(int error_code, const char* file_name, int line_num,
64+
const char *fmt, ...)
6265
{
63-
fprintf(error_stream, "cuda error: %d %s at %s:%d\n",
64-
error_code, " ", file_name, line_num);
66+
fprintf(error_stream, "ojph error %d at %s:%d\n",
67+
error_code, file_name, line_num);
68+
va_list args;
69+
va_start(args, fmt);
70+
fprintf(error_stream, fmt, args);
71+
va_end(args);
6572

66-
throw;
73+
throw std::runtime_error("ojph error");
6774
}
6875

69-
76+
////////////////////////////////////////////////////////////////////////////
77+
void warn(int warn_code, const char* file_name, int line_num,
78+
const char *fmt, ...)
79+
{
80+
fprintf(warning_stream, "ojph warning %d at %s:%d\n",
81+
warn_code, file_name, line_num);
82+
va_list args;
83+
va_start(args, fmt);
84+
fprintf(warning_stream, fmt, args);
85+
va_end(args);
86+
}
7087

7188

7289
}

0 commit comments

Comments
 (0)