Page 1 of 2
NSLog problem with PB6
Posted: Tue Aug 23, 2022 10:18 am
by deseven
This worked before 6.00 and C backend, but now it outputs garbage for some reason:
Code: Select all
ImportC ""
NSLog(format,message)
EndImport
Define format = CocoaMessage(0,0,"NSString stringWithString:$",@"%S")
Define message = CocoaMessage(0,0,"NSString stringWithString:$",@"test 123")
NSLog(format,message)
To see system logs, open Console app, press "Start" on top, input "PureBasic" in the search field, then run the code above.
I also tried using stringWithUTF8String and passing pointer to PB string directly - it all produces various types of garbage or invalid memory access.
Can anyone help?
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 10:56 am
by Wolfram
Code: Select all
ImportC ""
NSLog(format,message)
EndImport
Define format = CocoaMessage(0,0,"NSString stringWithString:$",@"%S")
Define message = CocoaMessage(0,0,"NSString stringWithString:$",@"test 123")
NSLog(format, @message)
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 10:58 am
by deseven
This outputs "\^B" to the system log (instead of "test 123").
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 11:23 am
by mk-soft
check it
Code: Select all
;-TOP
Macro CocoaString(NSString)
PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
EndMacro
ImportC ""
NSLog(format,message)
EndImport
Define format = CocoaMessage(0,0,"NSString stringWithString:$",@"%@") ; <- NSString %@, UTF8String %s
Define message = CocoaMessage(0,0,"NSString stringWithString:$",@"test 123")
Debug CocoaString(format)
Debug CocoaString(message)
NSLog(format, message)
Define *message = UTF8("Hello World!")
Define format2 = CocoaMessage(0,0,"NSString stringWithString:$",@"%s") ; <- NSString %@, UTF8String %s
NSLog(format2, *message)
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 11:31 am
by Wolfram
This Works too
Code: Select all
ImportC "-framework CoreFoundation"
CFStringCreateWithCString(alloc, cStr.p-utf8, encoding = $8000100)
EndImport
ImportC ""
NSLog(format, message)
EndImport
Define format = CFStringCreateWithCString(0, "%S")
Define message.s = "test 123"
NSLog(format, @message)
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 11:50 am
by mk-soft
Link Format Specifiers:
https://developer.apple.com/library/arc ... fiers.html
Encoding constants already defined
Code: Select all
ImportC "-framework CoreFoundation"
CFStringCreateWithCString(alloc, cStr.p-utf8, encoding = #kCFStringEncodingUTF8)
EndImport
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 11:59 am
by deseven
Hm... None of your solutions worked for me.
The one by Wolfram writes garbage to the system log.
The one by mk-soft gives me IMA (first NSLog call, so I commented it out) and also writes garbage (second NSLog call).
Do they work for you? I wonder what's the difference between our systems. I'm on 6.00 LTS macOS 11.6.5 by the way.
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 12:26 pm
by Wolfram
Yes, my second post is tested
PB6.beta7, macOS Catalina.
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 12:31 pm
by deseven
Could it be OS/compiler related then? Here's what I see with your code:

Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 12:32 pm
by mk-soft
macOS v12.3.1
Code: Select all
Macro CocoaString(NSString)
PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
EndMacro
Macro CocoaCString(NSString)
PeekS(CocoaMessage(0, NSString, "cStringUsingEncoding:", #NSUTF16LittleEndianStringEncoding))
EndMacro
Macro CocoaAllocString(String)
CocoaMessage(0, 0, "NSString stringWithBytes:", @String, "length:", StringByteLength(String), "encoding:", #NSUTF16LittleEndianStringEncoding)
EndMacro
ImportC ""
NSLog(format, message)
EndImport
Define format = CocoaAllocString("Output: %S")
Define message.s = "test 123"
Debug CocoaCString(format)
NSLog(format, @message)
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 12:45 pm
by deseven
And this one gives me "Output: %garbage%"

Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 1:34 pm
by mk-soft
Works here with High Sierra v10.13.6 (VM) and Monterey v12.3.1 (Host)
Console:
standard 14:28:43.374138+0200 PureBasic.0 Output: Test: 14:28:43
Code: Select all
Macro CocoaString(NSString)
PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
EndMacro
Macro CocoaCString(NSString)
PeekS(CocoaMessage(0, NSString, "cStringUsingEncoding:", #NSUTF16LittleEndianStringEncoding))
EndMacro
Macro CocoaAllocString(String)
CocoaMessage(0, 0, "NSString stringWithBytes:", @String, "length:", StringByteLength(String), "encoding:", #NSUTF16LittleEndianStringEncoding)
EndMacro
ImportC ""
NSLog(format, message)
EndImport
Define format = CocoaAllocString("Output: %S")
Define message.s = "Test: " + FormatDate("%HH:%II:%SS", Date())
NSLog(format, @message)
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 1:37 pm
by deseven
And this code always gives me "Output: \^B", even though the Date() is obviously changing.
Thanks guys, I guess there's something wrong with my system, I'll test on other machines to see if it works.
Re: NSLog problem with PB6
Posted: Tue Aug 23, 2022 3:16 pm
by deseven
Ok, looks like this happens only with arm/aarch64 compiler, x86_64 works fine (tested on Catalina and Big Sur). Can anyone who has M1-based machine try it as well, just to confirm?
Re: NSLog problem with PB6
Posted: Mon Oct 31, 2022 10:53 am
by deseven
Still no one with M1/M2 who can test it?