Page 1 of 1

Print to Console (log)

Posted: Thu Nov 07, 2019 2:18 pm
by Rinzwind
Any way to print to the macOS console output? (not terminal), Apple System Log?

NSLog

Thanks..

Re: Print to Console (log)

Posted: Thu Nov 07, 2019 2:42 pm
by wilbert
Rinzwind wrote:NSLog
I think using NSLog will be the easiest way.

Re: Print to Console (log)

Posted: Thu Nov 07, 2019 3:45 pm
by Rinzwind
How to call that from PB? NSString and variable arguments.

Re: Print to Console (log)

Posted: Thu Nov 07, 2019 3:48 pm
by wilbert
Rinzwind wrote:How to call that from PB? NSString and variable arguments.
I think the easiest way is just to log a normal PureBasic string and format that yourself.

Code: Select all

ImportC ""
  NSLog2(Format, Arg) As "_NSLog"
EndImport

Procedure NSLog(Message.s)
  Static Format.i
  If Format = 0
    CompilerIf #PB_Compiler_Unicode
      Format = CocoaMessage(0, 0, "NSString stringWithString:$", @"%S")
    CompilerElse
      Format = CocoaMessage(0, 0, "NSString stringWithString:$", @"%s")
    CompilerEndIf  
    CocoaMessage(0, Format, "retain")
  EndIf
  NSLog2(Format, @Message)  
EndProcedure


NSLog("NSLog testing")

Here's also an example with a CFString constant in the DataSection.
It might be a bit more difficult but I'm posting it also as a reference for myself on how to create a CFString constant at compile time. :wink:

Code: Select all

; >> PB x64 unicode <<

; External constants
!extern ___CFConstantStringClassReference

; Imports
ImportC ""
  NSLog2(Format, Arg) As "_NSLog"
EndImport

; Define CFString constant
DataSection
  
  !CFSTRData_Format:
  !db '%S',0
  
  !align 4
  CFSTR_Format:
  !dq ___CFConstantStringClassReference, 0x7c8
  !dq CFSTRData_Format, 2; Asciiz pointer and size
  
EndDataSection



Procedure NSLog(Message.s)
  NSLog2(?CFSTR_Format, @Message)  
EndProcedure

NSLog("NSLog testing")