0

I am trying to test a function (bar()) which uses a module specific print function (foo_print) to output to the console, seen below:

#define foo_print(...) foo_log(FOO_LOGTYPE_PRINT, ## __VA_ARGS__)

where foo_log() is defined as an item of type

typedef (*foo_log_fct)(mkqfs_logtype type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));

Without changing this definition, I need to access and store numeric values that are passed into foo_print inside of bar(). I need to do this because this is the only output of bar().

I am planning on creating a stub function my_foo_print(const char *fmt, ...) and redefining foo_print to call this instead, so that I can capture the output and make sure it is correct.

I have two questions:

  1. Is my function stub correct? Have I asked for the right arguments
  2. How can I access and store the variable arguments to this function without knowing how many there will be and what type? My understanding is that sequential calls to va_args(va_list, type) require you to know these things. Is there any way around this?
  • What is `bar()` in your question? Also why do you stringize `__VA_ARGS__`? – chqrlie Sep 25 '20 at 18:31
  • Bar is a print info function. It processes a filesystem struct and foo_prints different information depending on calculations it makes. I am trying to test these calculation outputs by getting the values passed to foo_print and compare them to expected. As to stringizing va_args, I am not sure, that just how it is in the module library – applepiemyguy Sep 25 '20 at 18:44
  • Should be `my_foo_print(mkqfs_logtype type, const char *fmt, ...)`. In other words, your function needs to match the `typedef`. – user3386109 Sep 25 '20 at 18:52
  • [Stringizing is a trick](https://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick) to handle the case where `foo_print` is called with no parameters, i.e. `foo_print()`. That trick is not needed here, since the arguments to `foo_print` can't be empty. There must be a format string. – user3386109 Sep 25 '20 at 18:57
  • What is this "console" you speak of? It looks like bar prints to stdout. Just create a pipe, call the function with file descriptor 1 writing into the pipe, and validate the output. – William Pursell Sep 25 '20 at 19:28

1 Answers1

0

No VA ARGS is the way. Every time you call it it will switch the argument that you are referring to

Matt Salem
  • 75
  • 8