generated from NOAA-OWP/owp-open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a logging function #643
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef NGEN_LOGGING_UTILS_H | ||
#define NGEN_LOGGING_UTILS_H | ||
|
||
#define MAX_STRING_SIZE 2048 | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
namespace logging { | ||
/** | ||
* Send debug output to std::cerr | ||
* @param msg The variable carries the humanly readable debug text info. | ||
*/ | ||
void debug(const char* msg); | ||
|
||
/** | ||
* Send info content to std::cerr | ||
* @param msg The variable carries the humanly readable info text. | ||
*/ | ||
void info(const char* msg); | ||
|
||
/** | ||
* Send warning to std::cerr | ||
* @param msg The variable carries the humanly readable warning message. | ||
*/ | ||
void warning(const char* msg); | ||
|
||
/** | ||
* Send error message to std::cerr | ||
* @param msg The variable carries the humanly readable error message. | ||
*/ | ||
void error(const char* msg); | ||
|
||
/** | ||
* Send critical message to std::cerr | ||
* @param msg The variable carries the humanly readable critical message. | ||
*/ | ||
void critical(const char* msg); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif //NGEN_LOGGING_UTILS_H | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_library(logging logging_utils.cpp) | ||
add_library(NGen::logging ALIAS logging) | ||
target_include_directories(logging PUBLIC ${PROJECT_SOURCE_DIR}/include/utilities) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <string> | ||
#include "logging_utils.h" | ||
|
||
namespace logging { | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
void debug(const char* msg) | ||
{ | ||
#ifndef NGEN_QUIET | ||
std::cerr<<"DEBUG: " << std::string(msg); | ||
#endif | ||
} | ||
|
||
void info(const char* msg) | ||
{ | ||
#ifndef NGEN_QUIET | ||
std::cerr<<"INFO: " << std::string(msg); | ||
#endif | ||
} | ||
|
||
void warning(const char* msg) | ||
{ | ||
#ifndef NGEN_QUIET | ||
std::cerr<<"WARNING: " <<std::string(msg); | ||
#endif | ||
} | ||
|
||
void error(const char* msg) | ||
{ | ||
std::cerr<<"ERROR: " <<std::string(msg); | ||
} | ||
|
||
void critical(const char* msg) | ||
{ | ||
std::cerr<<"CRITICAL: " <<std::string(msg); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include "gtest/gtest.h" | ||
#include <iostream> | ||
#include <cstring> | ||
#include <string> | ||
#include "logging_utils.h" | ||
|
||
using namespace logging; | ||
|
||
class loggingTest : public ::testing::Test { | ||
|
||
protected: | ||
|
||
loggingTest() {} | ||
|
||
~loggingTest() override {} | ||
|
||
void SetUp() override; | ||
|
||
void TearDown() override; | ||
|
||
}; | ||
|
||
void loggingTest::SetUp() { | ||
} | ||
|
||
void loggingTest::TearDown() { | ||
} | ||
|
||
//Test logging debug function | ||
TEST_F(loggingTest, TestDebug) | ||
{ | ||
const std::string test_str = "Debugging output messages.\n"; | ||
|
||
testing::internal::CaptureStderr(); | ||
debug(test_str.c_str()); | ||
|
||
const std::string cerr_output = testing::internal::GetCapturedStderr(); | ||
const std::string str = "DEBUG: Debugging output messages.\n"; | ||
|
||
#ifndef NGEN_QUIET | ||
EXPECT_EQ(cerr_output, str); | ||
#else | ||
EXPECT_EQ(cerr_output, ""); | ||
#endif | ||
} | ||
|
||
//Test logging info function | ||
TEST_F(loggingTest, TestInfo) | ||
{ | ||
const std::string test_str = "Runtime information messages.\n"; | ||
|
||
testing::internal::CaptureStderr(); | ||
info(test_str.c_str()); | ||
|
||
const std::string cerr_output = testing::internal::GetCapturedStderr(); | ||
const std::string str = "INFO: Runtime information messages.\n"; | ||
|
||
#ifndef NGEN_QUIET | ||
EXPECT_EQ(cerr_output, str); | ||
#else | ||
EXPECT_EQ(cerr_output, ""); | ||
#endif | ||
} | ||
|
||
//Test logging warning function | ||
TEST_F(loggingTest, TestWarning) | ||
{ | ||
const std::string test_str = "Runtime warning messages.\n"; | ||
|
||
testing::internal::CaptureStderr(); | ||
warning(test_str.c_str()); | ||
|
||
const std::string cerr_output = testing::internal::GetCapturedStderr(); | ||
const std::string str = "WARNING: Runtime warning messages.\n"; | ||
|
||
#ifndef NGEN_QUIET | ||
EXPECT_EQ(cerr_output, str); | ||
#else | ||
EXPECT_EQ(cerr_output, ""); | ||
#endif | ||
} | ||
|
||
//Test logging critical function | ||
TEST_F(loggingTest, TestCritical) | ||
{ | ||
const std::string test_str = "Critical runtime messages.\n"; | ||
|
||
testing::internal::CaptureStderr(); | ||
critical(test_str.c_str()); | ||
|
||
const std::string cerr_output = testing::internal::GetCapturedStderr(); | ||
const std::string str = "CRITICAL: Critical runtime messages.\n"; | ||
|
||
EXPECT_EQ(cerr_output, str); | ||
} | ||
|
||
//Test logging error function | ||
TEST_F(loggingTest, TestError) | ||
{ | ||
const std::string test_str = "An error has occurred during runtime.\n"; | ||
|
||
testing::internal::CaptureStderr(); | ||
error(test_str.c_str()); | ||
|
||
const std::string cerr_output = testing::internal::GetCapturedStderr(); | ||
const std::string str = "ERROR: An error has occurred during runtime.\n"; | ||
|
||
EXPECT_EQ(cerr_output, str); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, really any new functionality added should have some unit tests. This is pretty simple so the unit test doesn't have to be huge, but something that puts a message through each function and makes sure it comes out with the expected text would be good. You can look at this approach from one of @program-- 's old commits for an example of how to do that:
https://github.com/program--/ngen/blob/4a1d137bff0ff3324cfb2239ac1eaec72b66705d/test/forcing/CsvPerFeatureForcingProvider_Test.cpp#L202-L216
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. this should be done.