title: Is this a good error reporting solution for an embedded C app?
body: <p>Based on the answers to a previous <a href="https://stackoverflow.com/questions/1161278/does-anyone-have-a-good-guide-to-designing-and-error-tracking-control-system-for">question</a>, here's a design for a simple error reporting system for my embedded C app. I'd appreciate some feedback.</p>
  
  <p>My app has code forming an overall program plus several layers of lower-level libraries. At the lowest level, the library gets a byte to use for error codes, so 255 total errors. They'll be encoded in an enum, like:</p>
  
  <pre><code>enum lib1ErrorCodes {
      ERR_NO_ERROR,
      ERR_NO_CONNECTION,
      ...
      ERR_MISC
  };
  </code></pre>
  
  <p>This is passed up the chain either through a global variable:</p>
  
  <pre><code>unsigned char lib1ErrNo;
  </code></pre>
  
  <p>The next higher level contains error codes for the libraries it uses:</p>
  
  <pre><code>enum lib2ErrorCodes {
      ERR_NO_ERROR,
      ERR_LIB1,
      ERR_FILE_EXISTS,
      ...
      ERR_MISC
  }
  </code></pre>
  
  <p>Lib1's error is detected and is flagged in this level's error variable:</p>
  
  <pre><code>unsigned char lib2ErrNo = ERR_LIB1;
  </code></pre>
  
  <p>At the top level, when it becomes time to report all this to the user, these are detected:</p>
  
  <pre><code>if (lib3ErrNo == ERR_LIB2)
      if (lib2ErrNo == ERR_LIB1)
          printf("Error %d: %s", lib1ErrNo, lib1ErrDesc);
  </code></pre>
  
  <p>The only negatives I can think of in this scheme is the need to set aside a few error codes in each library to point to the libraries under it, and for the top-level program to include all these levels for their error codes.</p>
  
  <p>What's the appropriate way to do this, if this isn't it?</p>
  
  <p>I want:</p>
  
  <ul>
  <li>Unique error propagation all the way up to the top level so I can report it.</li>
  <li>Preferably not passing around large structures, as this is an embedded app.</li>
  </ul>