Problems with Unified Logging, StaticString, CustomStringConvertible and description [duplicate]












0















This question already has an answer here:




  • Using os_log to log function arguments, or other dynamic data

    1 answer




I'm wondering how I'm supposed to handle the following fairly common scenario using Unified Logging. Let's say I have an object of class Foo which I want to log. Foo implements the CustomStringConvertible protocol, so I can get a description of the object I can use in my logs:



class Foo: CustomStringConvertible {
var bar:Int = 1

var description: String {
return "<(type(of: self)): bar = (bar)>"
}
}

let myFoo = Foo()


If I call print(myFoo), I get a nice description of Foo. However, os_log(myFoo) won't work since description is not a StaticString. Is there a way to accomplish what I'm trying to do?










share|improve this question















marked as duplicate by Martin R swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 5:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Using os_log to log function arguments, or other dynamic data

      1 answer




    I'm wondering how I'm supposed to handle the following fairly common scenario using Unified Logging. Let's say I have an object of class Foo which I want to log. Foo implements the CustomStringConvertible protocol, so I can get a description of the object I can use in my logs:



    class Foo: CustomStringConvertible {
    var bar:Int = 1

    var description: String {
    return "<(type(of: self)): bar = (bar)>"
    }
    }

    let myFoo = Foo()


    If I call print(myFoo), I get a nice description of Foo. However, os_log(myFoo) won't work since description is not a StaticString. Is there a way to accomplish what I'm trying to do?










    share|improve this question















    marked as duplicate by Martin R swift
    Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 21 at 5:58


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • Using os_log to log function arguments, or other dynamic data

        1 answer




      I'm wondering how I'm supposed to handle the following fairly common scenario using Unified Logging. Let's say I have an object of class Foo which I want to log. Foo implements the CustomStringConvertible protocol, so I can get a description of the object I can use in my logs:



      class Foo: CustomStringConvertible {
      var bar:Int = 1

      var description: String {
      return "<(type(of: self)): bar = (bar)>"
      }
      }

      let myFoo = Foo()


      If I call print(myFoo), I get a nice description of Foo. However, os_log(myFoo) won't work since description is not a StaticString. Is there a way to accomplish what I'm trying to do?










      share|improve this question
















      This question already has an answer here:




      • Using os_log to log function arguments, or other dynamic data

        1 answer




      I'm wondering how I'm supposed to handle the following fairly common scenario using Unified Logging. Let's say I have an object of class Foo which I want to log. Foo implements the CustomStringConvertible protocol, so I can get a description of the object I can use in my logs:



      class Foo: CustomStringConvertible {
      var bar:Int = 1

      var description: String {
      return "<(type(of: self)): bar = (bar)>"
      }
      }

      let myFoo = Foo()


      If I call print(myFoo), I get a nice description of Foo. However, os_log(myFoo) won't work since description is not a StaticString. Is there a way to accomplish what I'm trying to do?





      This question already has an answer here:




      • Using os_log to log function arguments, or other dynamic data

        1 answer








      swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 23:45

























      asked Nov 20 at 23:14









      pondermatic

      3,52183851




      3,52183851




      marked as duplicate by Martin R swift
      Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 at 5:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Martin R swift
      Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 at 5:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          I'm having trouble understanding what part you find difficult, so here's a complete example and you can pick out of it whatever you need:



          import UIKit
          import os
          let mylog = OSLog(subsystem: "com.neuburg.matt", category: "testing")
          class Foo: CustomStringConvertible {
          var bar:Int = 1
          var description: String {
          return "<(type(of: self)): bar = (bar)>"
          }
          }
          class ViewController: UIViewController {
          override func viewDidLoad() {
          super.viewDidLoad()
          let foo = Foo()
          os_log("%{public}@", log: mylog, String(describing:foo))
          }
          }


          Prints:



          [testing] <Foo: bar = 1>


          ...which I believe was the goal, no?






          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I'm having trouble understanding what part you find difficult, so here's a complete example and you can pick out of it whatever you need:



            import UIKit
            import os
            let mylog = OSLog(subsystem: "com.neuburg.matt", category: "testing")
            class Foo: CustomStringConvertible {
            var bar:Int = 1
            var description: String {
            return "<(type(of: self)): bar = (bar)>"
            }
            }
            class ViewController: UIViewController {
            override func viewDidLoad() {
            super.viewDidLoad()
            let foo = Foo()
            os_log("%{public}@", log: mylog, String(describing:foo))
            }
            }


            Prints:



            [testing] <Foo: bar = 1>


            ...which I believe was the goal, no?






            share|improve this answer


























              1














              I'm having trouble understanding what part you find difficult, so here's a complete example and you can pick out of it whatever you need:



              import UIKit
              import os
              let mylog = OSLog(subsystem: "com.neuburg.matt", category: "testing")
              class Foo: CustomStringConvertible {
              var bar:Int = 1
              var description: String {
              return "<(type(of: self)): bar = (bar)>"
              }
              }
              class ViewController: UIViewController {
              override func viewDidLoad() {
              super.viewDidLoad()
              let foo = Foo()
              os_log("%{public}@", log: mylog, String(describing:foo))
              }
              }


              Prints:



              [testing] <Foo: bar = 1>


              ...which I believe was the goal, no?






              share|improve this answer
























                1












                1








                1






                I'm having trouble understanding what part you find difficult, so here's a complete example and you can pick out of it whatever you need:



                import UIKit
                import os
                let mylog = OSLog(subsystem: "com.neuburg.matt", category: "testing")
                class Foo: CustomStringConvertible {
                var bar:Int = 1
                var description: String {
                return "<(type(of: self)): bar = (bar)>"
                }
                }
                class ViewController: UIViewController {
                override func viewDidLoad() {
                super.viewDidLoad()
                let foo = Foo()
                os_log("%{public}@", log: mylog, String(describing:foo))
                }
                }


                Prints:



                [testing] <Foo: bar = 1>


                ...which I believe was the goal, no?






                share|improve this answer












                I'm having trouble understanding what part you find difficult, so here's a complete example and you can pick out of it whatever you need:



                import UIKit
                import os
                let mylog = OSLog(subsystem: "com.neuburg.matt", category: "testing")
                class Foo: CustomStringConvertible {
                var bar:Int = 1
                var description: String {
                return "<(type(of: self)): bar = (bar)>"
                }
                }
                class ViewController: UIViewController {
                override func viewDidLoad() {
                super.viewDidLoad()
                let foo = Foo()
                os_log("%{public}@", log: mylog, String(describing:foo))
                }
                }


                Prints:



                [testing] <Foo: bar = 1>


                ...which I believe was the goal, no?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 0:03









                matt

                323k45519720




                323k45519720















                    Popular posts from this blog

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen