using namespace snort;
-bool trace_enabled(Trace mask, Trace flags)
-{ return mask & flags; }
-
-bool trace_enabled(Trace mask)
-{ return mask; }
-
template <int (output)(const char*, FILE*)>
-static inline void trace_vprintf(const char* name, Trace mask, const char* file, int line,
- Trace flags, const char* fmt, va_list ap)
+static inline void trace_vprintf(const char* name, const char* fmt, va_list ap)
{
- if ( !trace_enabled(mask, flags) )
- return;
-
char buf[STD_BUF];
int buf_len = sizeof(buf);
char* buf_ptr = buf;
- int size;
if (name)
{
- size = snprintf(buf, buf_len, "%s: ", name);
- if ( size >= buf_len )
- size = buf_len - 1;
- if ( size > 0 )
- {
- buf_ptr += size;
- buf_len -= size;
- }
- }
-
- if ( file )
- {
- size = snprintf(buf_ptr, buf_len, "%s:%d: ", file, line);
+ int size = snprintf(buf, buf_len, "%s: ", name);
if ( size >= buf_len )
size = buf_len - 1;
if ( size > 0 )
output(buf, stdout);
}
-void trace_vprintf(const char* name, Trace mask, const char* file, int line,
- Trace flags, const char* fmt, va_list ap)
+void trace_vprintf(const char* name, const char* fmt, va_list ap)
{
- trace_vprintf<fputs>(name, mask, file, line, flags, fmt, ap);
+ trace_vprintf<fputs>(name, fmt, ap);
}
#ifdef UNIT_TEST
{
{
sx(trace_log(testing, "my message")),
- "trace_print<trace_vprintf>(\"testing\", testing_trace, nullptr, 0, \"my message\")"
+ "trace_print<trace_vprintf>(\"testing\", testing_trace, \"my message\")"
},
{
sx(trace_log(testing, my_flags, "my message")),
- "trace_print<trace_vprintf>(\"testing\", testing_trace, nullptr, 0, my_flags, \"my message\")"
+ "trace_print<trace_vprintf>(\"testing\", testing_trace, my_flags, \"my message\")"
},
{
sx(trace_logf(testing, "%s %s", "my", "message")),
- "trace_printf<trace_vprintf>(\"testing\", testing_trace, nullptr, 0, \"%s %s\", \"my\", \"message\")"
+ "trace_printf<trace_vprintf>(\"testing\", testing_trace, \"%s %s\", \"my\", \"message\")"
},
{
sx(trace_logf(testing, my_flags, "%s %s", "my", "message")),
- "trace_printf<trace_vprintf>(\"testing\", testing_trace, nullptr, 0, my_flags, \"%s %s\", \"my\", \"message\")"
- },
- {
- sx(trace_debug(testing, "my message")), "trace_print<trace_vprintf>(\"testing\", testing_trace, " sx(__FILE__) ", " sx(__LINE__) ", \"my message\")"
- },
- {
- sx(trace_debug(testing, my_flags, "my message")), "trace_print<trace_vprintf>(\"testing\", testing_trace, " sx(__FILE__) ", " sx(__LINE__) ", my_flags, \"my message\")"
- },
- {
- sx(trace_debugf(testing, "%s %s", "my", "message")), "trace_printf<trace_vprintf>(\"testing\", testing_trace, " sx(__FILE__) ", " sx(__LINE__) ", \"%s %s\", \"my\", \"message\")"
- },
- {
- sx(trace_debugf(testing, my_flags, "%s %s", "my", "message")), "trace_printf<trace_vprintf>(\"testing\", testing_trace, " sx(__FILE__) ", " sx(__LINE__) ", my_flags, \"%s %s\", \"my\", \"message\")"
+ "trace_printf<trace_vprintf>(\"testing\", testing_trace, my_flags, \"%s %s\", \"my\", \"message\")"
}
};
CHECK( !strcmp(cases[1].expected, cases[1].test) );
CHECK( !strcmp(cases[2].expected, cases[2].test) );
CHECK( !strcmp(cases[3].expected, cases[3].test) );
- CHECK( !strcmp(cases[4].expected, cases[4].test) );
- CHECK( !strcmp(cases[5].expected, cases[5].test) );
- CHECK( !strcmp(cases[6].expected, cases[6].test) );
- CHECK( !strcmp(cases[7].expected, cases[7].test) );
}
#undef trace_print
CHECK( !strcmp(testing_dump, "testing: my other masked message") );
}
-TEST_CASE("trace_debug", "[trace]")
-{
- Trace TRACE_NAME(testing) = TRACE_SECTION_2 | TRACE_SECTION_3;
-
- testing_dump[0] = '\0';
- trace_debug(testing, "my message"); CHECK( !strcmp(testing_dump, "testing: " __FILE__ ":" sx(__LINE__) ": my message") );
-
- testing_dump[0] = '\0';
- trace_debug(testing, TRACE_SECTION_1, "my masked message");
- CHECK( testing_dump[0] == '\0' );
-
- testing_dump[0] = '\0';
- trace_debug(testing, TRACE_SECTION_2, "my other masked message"); CHECK( !strcmp(testing_dump, "testing: " __FILE__ ":" sx(__LINE__) ": my other masked message") );
-}
-
-TEST_CASE("trace_debugf", "[trace]")
-{
- Trace TRACE_NAME(testing) = TRACE_SECTION_2 | TRACE_SECTION_3;
-
- testing_dump[0] = '\0';
- trace_debugf(testing, "%s %s", "my", "message"); CHECK( !strcmp(testing_dump, "testing: " __FILE__ ":" sx(__LINE__) ": my message") );
-
- testing_dump[0] = '\0';
- trace_debugf(testing, TRACE_SECTION_1, "%s %s %s", "my", "masked", "message");
- CHECK( testing_dump[0] == '\0' );
-
- testing_dump[0] = '\0';
- trace_debugf(testing, TRACE_SECTION_2, "%s %s %s %s", "my", "other", "masked", "message"); CHECK( !strcmp(testing_dump, "testing: " __FILE__ ":" sx(__LINE__) ": my other masked message") );
-}
-
TEST_CASE("safety", "[trace]")
{
Trace TRACE_NAME(testing) = TRACE_SECTION_2 | TRACE_SECTION_3;
typedef uint64_t Trace;
-bool trace_enabled(Trace mask);
-bool trace_enabled(Trace mask, Trace flags);
-
#define TRACE_NAME(name) name##_trace
#ifdef DEBUG_MSGS
-void trace_vprintf(const char* name, Trace mask, const char* file, int line,
- Trace flags, const char* fmt, va_list);
+void trace_vprintf(const char* name, const char* fmt, va_list);
+
+static inline bool trace_enabled(Trace mask, Trace flags)
+{ return mask & flags; }
+
+static inline bool trace_enabled(Trace mask)
+{ return mask; }
-template <void (trace_vprintf)(const char*, Trace, const char*, int, Trace, const char*, va_list)>
-static inline void trace_printf(const char* name, Trace mask, const char* file, int line,
- Trace flags, const char* fmt, ...) __attribute__((format (printf, 6, 7)));
+template <void (trace_vprintf)(const char*, const char*, va_list)>
+static inline void trace_printf(const char* name, Trace mask, Trace flags, const char* fmt, ...)
+ __attribute__((format (printf, 4, 5)));
-template <void (trace_vprintf)(const char*, Trace, const char*, int, Trace, const char*, va_list) = trace_vprintf>
-static inline void trace_printf(const char* name, Trace mask, const char* file, int line,
- Trace flags, const char* fmt, ...)
+template <void (trace_vprintf)(const char*, const char*, va_list) = trace_vprintf>
+static inline void trace_printf(const char* name, Trace mask, Trace flags, const char* fmt, ...)
{
+ if (!trace_enabled(mask, flags))
+ return;
+
va_list ap;
va_start(ap, fmt);
- trace_vprintf(name, mask, file, line, flags, fmt, ap);
+ trace_vprintf(name, fmt, ap);
va_end(ap);
}
-template <void (trace_vprintf)(const char*, Trace, const char*, int, Trace, const char*, va_list)>
-static inline void trace_printf(const char* name, Trace mask, const char* file,
- int line, const char* fmt, ...) __attribute__((format (printf, 5, 6)));
+template <void (trace_vprintf)(const char*, const char*, va_list)>
+static inline void trace_printf(const char* name, Trace mask, const char* fmt, ...)
+ __attribute__((format (printf, 3, 4)));
-template <void (trace_vprintf)(const char*, Trace, const char*, int, Trace, const char*, va_list) = trace_vprintf>
-static inline void trace_printf(const char* name, Trace mask, const char* file,
- int line, const char* fmt, ...)
+template <void (trace_vprintf)(const char*, const char*, va_list) = trace_vprintf>
+static inline void trace_printf(const char* name, Trace mask, const char* fmt, ...)
{
+ if (!trace_enabled(mask))
+ return;
+
va_list ap;
va_start(ap, fmt);
- trace_vprintf(name, mask, file, line, UINT64_MAX, fmt, ap);
+ trace_vprintf(name, fmt, ap);
va_end(ap);
}
-template <void (trace_vprintf)(const char*, Trace, const char*, int, Trace, const char*, va_list) = trace_vprintf>
-static inline void trace_print(const char* name, Trace mask, const char* file,
- int line, const char* msg)
+template <void (trace_vprintf)(const char*, const char*, va_list) = trace_vprintf>
+static inline void trace_print(const char* name, Trace mask, const char* msg)
{
- trace_printf<trace_vprintf>(name, mask, file, line, UINT64_MAX, "%s", msg);
+ trace_printf<trace_vprintf>(name, mask, UINT64_MAX, "%s", msg);
}
-template <void (trace_vprintf)(const char*, Trace, const char*, int, Trace, const char*, va_list) = trace_vprintf>
-static inline void trace_print(const char* name, Trace mask, const char* file,
- int line, Trace flags, const char* msg)
+template <void (trace_vprintf)(const char*, const char*, va_list) = trace_vprintf>
+static inline void trace_print(const char* name, Trace mask, Trace flags, const char* msg)
{
- trace_printf<trace_vprintf>(name, mask, file, line, flags, "%s", msg);
+ trace_printf<trace_vprintf>(name, mask, flags, "%s", msg);
}
#define trace_print trace_print<trace_vprintf>
#define trace_printf trace_printf<trace_vprintf>
#define trace_log(tracer, ...) \
- trace_print(#tracer, tracer##_trace, nullptr, 0, __VA_ARGS__)
+ trace_print(#tracer, tracer##_trace, __VA_ARGS__)
#define trace_log_wo_name(tracer, ...) \
- trace_print(nullptr, tracer##_trace, nullptr, 0, __VA_ARGS__)
+ trace_print(nullptr, tracer##_trace, __VA_ARGS__)
#define trace_logf(tracer, ...) \
- trace_printf(#tracer, tracer##_trace, nullptr, 0, __VA_ARGS__)
+ trace_printf(#tracer, tracer##_trace, __VA_ARGS__)
#define trace_logf_wo_name(tracer, ...) \
- trace_printf(nullptr, tracer##_trace, nullptr, 0, __VA_ARGS__)
-
-#define trace_debug(tracer, ...) \
- trace_print(#tracer, tracer##_trace, __FILE__, __LINE__, __VA_ARGS__)
-
-#define trace_debugf(tracer, ...) \
- trace_printf(#tracer, tracer##_trace, __FILE__, __LINE__, __VA_ARGS__)
+ trace_printf(nullptr, tracer##_trace, __VA_ARGS__)
#else
+
#define trace_log(tracer, ...)
#define trace_log_wo_name(tracer, ...)
#define trace_logf(tracer, ...)
#define trace_logf_wo_name(tracer, ...)
-#define trace_debug(tracer, ...)
-#define trace_debugf(tracer, ...)
#endif
#endif
-