Please convert variable type C++ function to swift language

I went through online but I haven't found way to convert variable type function handling. Can any one help to convert below C++ API to swift language:

Code Block
void netops_log(log_level level, const char *format, ...);
void netops_log(log_level level, const char *format, ...)
{
  char msg[4096] = "";
  va_list args;
  va_start(args, format);
  vsnprintf(msg, 4095, format, args);
  LogMsg(level, msg);
  va_end(args);
}



Here, LogMsg(log_level level, const void *msg) is C++ api in a library. Thanks.


You can write a C-wrapper for LogMsg like this:
Code Block
void vnetops_log(log_level level, const char *format, va_list args) {
char msg[4096] = "";
vsnprintf(msg, 4095, format, args);
LogMsg(level, msg);
}

(Assuming you know how to write a header for this function and how to import it into Swift.)

And then you can write a Swift-side wrapper as follows:
Code Block
func netopsLog(level: log_level, format: String, _ args: CVarArg...) {
withVaList(args) {vaList in
vnetops_log(level, format, vaList)
}
}



Or else, if you can directly import LogMsg into Swift, you can write something equivalent to netops_log in Swift:
Code Block
func netops_log(level: log_level, format: String, _ args: CVarArg...) {
LogMsg(level, String(format: format, arguments: args))
}



Thanks for reply. I am trying use your second suggestion by calling C++ API LogMsg() from swift as below. It is working if I send only string without any arguments. But it is not working if I have variable args. So I want to convert String(format: format, arguments: args) in to void* type. But I am failing. Is there any way to convert String(format: format, arguments: args) in void pointer type before passing to LogMsg()

Code Block
func netops_log(level: log_level, format: String, _ args: CVarArg...) {
LogMsg(level, String(format: format, arguments: args))
}


LogMsg() declaration is as below in C++ lib:
Code Block
int LogMsg(int severity, const void *data);

It is working if I send only string without any arguments. But it is not working if I have variable args.

It is an odd behavior and I do not see what might be the issue. Can you clarify what is not working?
  • It does not compile

  • It does compile but causes runtime error

  • It does compile and no runtime error, but shows unexpected result

Also, you should better include all the relevant definitions. For example log_level.
One cannot imagine someone would ever declare a function as int LogMsg(int severity, const void *data); where it takes string.

Sorry improper inputs. It does compile and no runtime error, but shows unexpected result

int LogMsg(int severity, const void *data); is the C++ library API, which dumps log to my custom file. Here, int severity indicates severity which decides based on enum log_level and const void *data indicates message.
enum log_level looks like as below:
Code Block
enum log_level{
  LOGLEVEL_NONE = 0,
  LOGLEVEL_ERROR,
  LOGLEVEL_WARNING,
  LOGLEVEL_INFO,
LOGLEVEL_DEBUG
};



When I am calling like netops_log(level: LOGLEVEL_DEBUG, format: "Hi, I am in Main") in my swift, this message showing properly in the log.
But when am calling like netops_log(level: LOGLEVEL_DEBUG, format: "Hi, I am in Main : %s", stringValue) in my swift then I am getting some garbage value for 'stringValue' in the log. So I am missed any thing?

my netops_log API in swift:
Code Block
func netops_log(level: log_level, format: String, _ args: CVarArg...) {
LogMsg(level, String(format: format, arguments: args))
}


when am calling like netopslog(level: LOGLEVELDEBUG, format: "Hi, I am in Main : %s", stringValue) in my swift then I am getting some garbage value for 'stringValue' in the log. 

Thanks for clarifying. And sorry, I have forgotten to note one thing.
The format string may be a little bit different than a C-printf format.

When you want to include Swift String, you use %@ instead of %s:
Code Block
netops_log(level: LOGLEVEL_DEBUG, format: "Hi, I am in Main : %@", stringValue)


Please try.

Thanks for quick response. I got '@' as output with %@. I. have tried with first approach also as below. But the result is same. I am getting properly value in console if did with os_log()

Code Block
void vnetops_log(log_level level, const char *format, va_list args) {
char msg[4096] = "";
vsnprintf(msg, 4095, format, args);
LogMsg(level, msg);
}


Code Block
func netopsLog(level: log_level, format: String, _ args: CVarArg...) {
withVaList(args) {vaList in
vnetops_log(level, format, vaList) }
}


Is there any thing wrong with withVaList() or Is there any problem with way of calling netopsLog() with args?

 I got '@' as output with %@.

Sorry, I cannot reproduce the issue. The code String(format: format, arguments: args) would never generate such an output and I think I cannot help any more.
Please convert variable type C++ function to swift language
 
 
Q