Skip to content
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

Added print callback/redirect support to the C library to route C prints to glog in C++. #63

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

adamshapiro0
Copy link
Contributor

@adamshapiro0 adamshapiro0 commented Aug 20, 2024

New Features

  • Added optional user print callback to C library

Changes

  • Route all debug and error prints from the C library through glog in C++, rather than printing directly to stderr

Fixed

  • Fixed deprecated upload/download artifact actions in CI script

@adamshapiro0 adamshapiro0 self-assigned this Aug 20, 2024
@@ -236,11 +282,18 @@ void Polaris_Free(PolarisContext_t* context) { CloseSocket(context, 1); }

/******************************************************************************/
void Polaris_SetLogLevel(int log_level) {
#if !defined(POLARIS_NO_PRINT)
#if !POLARIS_NO_PRINT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure we dont want ifndef....this just invest the value of POLARIS_NO_PRINT (which i guess by default is 0?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little more flexible in practice. !defined(POLARIS_NO_PRINT) would disable prints if the POLARIS_NO_PRINT macro is defined, no matter what its value is.

If you don't use defined() and just use the macro in an expression like !POLARIS_NO_PRINT and the macro is not defined, the compiler will treat it as 0. So in this case you get:

  • Don't define POLARIS_NO_PRINT at all -- Prints enabled
  • #define POLARIS_NO_PRINT 0 -- Prints enabled
  • #define POLARIS_NO_PRINT 1 -- Prints disabled

This makes it a little easier to turn prints on/off in a CMakeLists.txt file for example by having it change the value it uses in a -DPOLARIS_NO_PRINT=${foo} statement instead of having to remove the line entirely.

if (str_length != 0) {
str[str_length++] = '\0';
if (__print_callback) {
__print_callback("polaris.c", __LINE__, POLARIS_LOG_LEVEL_TRACE, str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if using line, we should use FILE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the above should be the c style thing with two underscore in front and behind, github is formatting it instead lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, yes. We did this just to simplify/shorten the print since __FILE__ always includes the full path to the file (src/...) and this is the only source file in the repo that prints anything. Unfortunately there's no macro to get just the filename, and you can't do string manipulation in the pre-processor.

I can switch it to __FILE__ if you think that's better?

}
#endif

/******************************************************************************/
#if !defined(P1_NO_PRINT) && defined(POLARIS_USE_TLS)
#if !P1_NO_PRINT && POLARIS_USE_TLS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ifndef

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my explanation above. This makes it a lot easier to set these macros in CMake or Bazel by changing the value of a -D statement instead of having to actually conditionally remove the -D altogether.

DEFINE_string(
polaris_api_hostname, "",
"Specify an alternate hostname to use when connecting to the Polaris "
"authentication API server. If blank, use the default hostname.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add to this comment that changes this is almost never necessary (for the public)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anathan how's this?

DEFINE_string(
    polaris_api_hostname, "",
    "Specify an alternate hostname to use when connecting to the Polaris "
    "authentication API server. Specifying a custom API hostname is not "
    "common, and is intended primarily for development purposes. If blank, use "
    "the default hostname.");

DEFINE_string(
polaris_hostname, "",
"Specify an alternate hostname to use when connecting to the Polaris "
"corrections network. If blank, use the default hostname.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hint that is useful for regional endpoints in the EU and APAC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anathan Is this clear, or would you suggest anything else?

DEFINE_string(
    polaris_hostname, "",
    "Specify an alternate hostname to use when connecting to the Polaris "
    "corrections network. This may be useful when connecting to a regional "
    "endpoint in the EU or APAC. If blank, use the default hostname.");

Copy link
Member

@anathan anathan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only nit is that it makes it so we have to print debug prints as lines, which introduces overhead in some embedded cases where you may be printing out as your loop goes. This is overall though a better structure.

@adamshapiro0
Copy link
Contributor Author

my only nit is that it makes it so we have to print debug prints as lines, which introduces overhead in some embedded cases where you may be printing out as your loop goes. This is overall though a better structure.

@anathan Not totally sure I follow the concern here? You can still print from within a loop if you want. Do you have an example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants