NSLog problem with PB6

Mac OSX specific forum
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

NSLog problem with PB6

Post 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?
Wolfram
Enthusiast
Enthusiast
Posts: 604
Joined: Thu May 30, 2013 4:39 pm

Re: NSLog problem with PB6

Post 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)
macOS Catalina 10.15.7
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post by deseven »

This outputs "\^B" to the system log (instead of "test 123").
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: NSLog problem with PB6

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Wolfram
Enthusiast
Enthusiast
Posts: 604
Joined: Thu May 30, 2013 4:39 pm

Re: NSLog problem with PB6

Post 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)

macOS Catalina 10.15.7
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: NSLog problem with PB6

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post 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.
Wolfram
Enthusiast
Enthusiast
Posts: 604
Joined: Thu May 30, 2013 4:39 pm

Re: NSLog problem with PB6

Post by Wolfram »

Yes, my second post is tested
PB6.beta7, macOS Catalina.
macOS Catalina 10.15.7
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post by deseven »

Could it be OS/compiler related then? Here's what I see with your code:

Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: NSLog problem with PB6

Post 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)

Last edited by mk-soft on Tue Aug 23, 2022 1:35 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post by deseven »

And this one gives me "Output: %garbage%" :)
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: NSLog problem with PB6

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post 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.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post 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?
User avatar
deseven
Enthusiast
Enthusiast
Posts: 367
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: NSLog problem with PB6

Post by deseven »

Still no one with M1/M2 who can test it?
Post Reply