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

feat: add pactffi_log_to_file for consumer/verifier #428

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

YOU54F
Copy link
Member

@YOU54F YOU54F commented Jan 9, 2023

Add ability to log to file, via pactffi_log_to_file

Will go someway to address pact-foundation/pact-js#954

(will need adding into pact-js)

Passed in via an optional param, to getFfiLib which is called by a couple of functions, as an optional param. Wonder if it would be better to have these in an opts object that can take named params.

note, the option used to be called log in pact-js v2 and is moved to deprecated now. should we retain log or use a more descriptive logFile

https://github.com/pact-foundaton/pact-js/tree/9.x.x#constructor

edit

need to add some appropriate tests and refactor based on review comments :)

native/ffi.cc Outdated Show resolved Hide resolved
src/ffi/index.ts Outdated
@@ -10,20 +10,40 @@ export const PACT_FFI_VERSION = '0.3.19';
let ffi: typeof ffiLib;
let ffiLogLevel: LogLevel;

const initialiseFfi = (logLevel: LogLevel): typeof ffi => {
const initialiseFfi = (logLevel: LogLevel, logFile?: string): typeof ffi => {
Copy link
Contributor

@TimothyJones TimothyJones Jan 9, 2023

Choose a reason for hiding this comment

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

I think this would be better if it were more explicit - either you have a log file, or you don't. The current form means that it's easy to misread the intermediate functions that don't touch log file.

The guideline I would recommend is that optional parameters exist only at a boundary - and their presence (or not) is handled in the function where they are optional.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is now moved and updated to

export const getFfiLib = (
  logLevel: LogLevel = DEFAULT_LOG_LEVEL,
  logFile: string | undefined = undefined
): typeof ffi => {

src/ffi/index.ts Outdated Show resolved Hide resolved
src/ffi/index.ts Outdated
);
const res = ffiLib.pactffiLogToFile(
logFile,
convertLogLevelToFfiLoglevelFilter[logLevel]
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if convertLogLevelToFfiLoglevelFilter[logLevel] is undefined?

Copy link
Contributor

@TimothyJones TimothyJones Jan 9, 2023

Choose a reason for hiding this comment

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

I think this whole inlined convertLogLevelToFfiLoglevelFilter constant would be better replaced with a switch - then it would introduce no new concepts, and would give you error handling for free.

In general, if you need a really long name for something then it's a good signal that there's an alternative design that would be better.

Copy link
Member Author

Choose a reason for hiding this comment

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

bad things probably 😂 or possibly, I don't know. Makes sense to test all the cases. I just did the bare minimum I could to get logging to file into pact-js when I was in the middle of diving into learning about FFI interfaces, so it is most certainly not the correct way, nor finished, I should move it to a draft and then it can be picked up either by me, or others if they feel inclined

Copy link
Member Author

Choose a reason for hiding this comment

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

If it is undefined, napi will throw an error as we are expecting a numerical argument

Napi::Value PactffiLogToFile(const Napi::CallbackInfo& info) {
   // return: int
   Napi::Env env = info.Env();

  if (info.Length() < 2) {
    throw Napi::Error::New(env, "PactffiLogToFile(envVar) received < 2 arguments");
  }

    if (!info[0].IsString()) {
    throw Napi::Error::New(env, "PactffiLogToFile(envVar) expected a string");
  }

  if (!info[1].IsNumber()) {
    throw Napi::Error::New(env, "PactffiLogToFile(envVar) expected a number");
  }

I've also retained the existing FfiLogLevelFilter[logLevel] rather than needing to add a new function convertLogLevelToFfiLoglevelFilter

@TimothyJones
Copy link
Contributor

Looks alright to me. I left a number of style comments inline.

feat: add pactffi_log_to_file for consumer/verifier

Note that this ends up in the changelog, and this message describes the internals of the change, not how users of pact-js-core will see the change. I think it should describe how the users of pact-js-core will see the change.

Passed in via an optional param, to getFfiLib which is called by a couple of functions, as an optional param. Wonder if it would be better to have these in an opts object that can take named params.

See my comments inline. I don't think it's a good idea to pass optional parameters down to child functions.

I also think that introducing an options object just for this seems unnecessary (and is kind of a workaround for the child function problem). Is there some reason you can't use the same pattern that the rest of the options uses?

@YOU54F
Copy link
Member Author

YOU54F commented May 2, 2023

Thanks for the review dude will address these when I get time :)

@YOU54F YOU54F marked this pull request as draft May 2, 2023 20:21
@YOU54F YOU54F force-pushed the add_ffi_log_to_file branch 2 times, most recently from 6ea56d5 to 9acf466 Compare June 25, 2024 21:51
@YOU54F YOU54F force-pushed the add_ffi_log_to_file branch 2 times, most recently from f4abc39 to c48d46f Compare September 6, 2024 14:22
@YOU54F YOU54F force-pushed the add_ffi_log_to_file branch from c48d46f to 02c8d19 Compare September 6, 2024 15:26
@YOU54F YOU54F marked this pull request as ready for review September 6, 2024 15:46
@YOU54F
Copy link
Member Author

YOU54F commented Sep 6, 2024

feat: add pactffi_log_to_file for consumer/verifier

Note that this ends up in the changelog, and this message describes the internals of the change, not how users of pact-js-core will see the change. I think it should describe how the users of pact-js-core will see the change.

Updates to reflect the end user facing argument that has been added, and to which functions they have been added, which is more sensible 👍🏾 thanks for the comment, totally agree

@YOU54F YOU54F merged commit de237c4 into master Sep 6, 2024
67 checks passed
@YOU54F YOU54F deleted the add_ffi_log_to_file branch September 6, 2024 15:59
break;
default:
std::string err = "pact-js-core C integration: Unable to parse log level number: ";
err += number;
Copy link
Member

Choose a reason for hiding this comment

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

Is err defined somewhere?

);
logger.warn(`The new requested log level '${logLevel}' will be ignored`);
Copy link
Member

Choose a reason for hiding this comment

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

Are we missing handling the error in this scenario (somebody initialising the FFI more than once with a different log level)?

@mefellows
Copy link
Member

Sorry for the late response - just added a couple more comments now, but thanks for the initiative and moving this forward!

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.

3 participants