News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Typing __FILE__, __LINE__ all the time

Started by Debbie2, March 05, 2006, 03:23:02 AM

Previous topic - Next topic

Debbie2

Don't you guys get tired of typing

db_query($sql, __FILE__, __LINE__);

all the time? Why can't it be made into this instead:

db_query($sql);

Is PHP not capable of doing proper stack traces or something?

    [/size][/list]
    [/

    Thantos

    Its so when we do any type of error logging we know what file and line the error occured on.  Errors such as database errors aren't actually shown to the user and have to be looked at later.

    Debbie2

    Would it be possible to get this information from debug_backtrace() without having to pass __FILE__ and __LINE__ around?

      [/size][/list]
      [/

      Thantos

      Not really no.  All that'll do is tell us that the error occurred in db_query ( or more likely db_error ) but thats not helpful at all since thats not where the error originated.

      Debbie2

      I just tested debug_backtrace() and it seems like it could do what __FILE__ and __LINE__ are doing right now...

      $ cat trace.php
      <?
      function print_trace() {
              foreach (debug_backtrace() as $trace) {
                      print "function ".$trace['function'].", file ".$trace['file'].", line ".$trace['line']."\n";
              }
      }
      ?>
      $ cat main.php
      <?
      include('trace.php');
      print_trace();
      ?>
      $ php main.php
      function print_trace, file /tmp/animaro/main.php, line 3


      The function print_trace() knew that it was called from main.php line 3, even though __FILE__ and __LINE__ were not passed to it.

        [/size][/list]
        [/

        Thantos

        Ok I misread it.  So yes it appears it does contain the information abit in a less accessible form.  If you look at the source files you'll see that the file and line information is used in db_query() and if there is an error in db_error().
        Now if __FILE__ and __LINE__ in PHP is anything like the same in C++ its much faster run time to use those constants as opposed to calling a function just to pull out the same information.  This becomes truer as you get deeper nestings.
        And really you get used to typing __FILE__, __LINE__ so it only takes a second.

        Debbie2

        QuoteNow if __FILE__ and __LINE__ in PHP is anything like the same in C++ its much faster run time to use those constants as opposed to calling a function just to pull out the same information.  This becomes truer as you get deeper nestings.

        The performance overhead of using debug_backtrace() should be negligible (assuming there even is any---I don't know how it works in PHP) considering that the file and line information is only needed if an error occurred, which usually doesn't happen.

        QuoteAnd really you get used to typing __FILE__, __LINE__ so it only takes a second.

        For me it gets tiresome to repeat it all the time. And sometimes I end up forgetting it and then my code gives me a warning that I didn't pass enough arguments to db_query() which I have to go back and fix.

          [/size][/list]
          [/

          sm2k

          From a performance standpoint it would actually be BETTER to use debug_trace when there actually is an error than to have to use __FILE__ and __LINE__ on every single query.  And anything that reduces redundant code is ok in my book!

          kegobeer

          debug_backtrace wasn't added until PHP 4.3.0.  SMF is designed to run with PHP 4.1.0, so using debug_backtrace really isn't an option.  Unless of course you want to write a debug_backtrace function.  ;)
          "The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
          Posting and you (Click "WATCH THIS MOVIE")

          Debbie2

          How about modifying Subs.php like this?

          function db_query($db_string, $file = '', $line = 0) {
              if ($file == '') {
                  # Auto-detect $file and $line
                  $trace = debug_backtrace()[0];
                  $file = $trace['file'];
                  $line = $trace['line'];
              }
          }


          This would allow people who write SMF plugins to just call db_query() with only the first argument.

          Then again, it could introduce weird errors when people try to use pre-PHP 4.3.0 versions.

            [/size][/list]
            [/

            Advertisement: