Print to Console (log)

Mac OSX specific forum
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Print to Console (log)

Post by Rinzwind »

Any way to print to the macOS console output? (not terminal), Apple System Log?

NSLog

Thanks..
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print to Console (log)

Post by wilbert »

Rinzwind wrote:NSLog
I think using NSLog will be the easiest way.
Windows (x64)
Raspberry Pi OS (Arm64)
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Print to Console (log)

Post by Rinzwind »

How to call that from PB? NSString and variable arguments.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Print to Console (log)

Post 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")
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply