Introducing libmerr

libmerr is an error tracking static library for C99+. During this involuntary vacation, I have spent some time taking code out of the Heterogeneous-Memory Storage Engine (HSE).

C is a notoriously bad language when it comes to error handling. Many system/POSIX/UNIX APIs will return various values on error or set errno(3). An integer error value can generally tell you the reason an error occurred, but it doesn’t provide extra information like file, line number, or any extra context.

To overcome this issue, the authors of HSE came up with merr. An merr_t is an error value which can encode all this information in just a 64-bit unsigned integer. This repository is essentially a copy and paste of what exists within HSE. Because of that you will see the original Micron Technology, Inc. copyright in addition to the dual licensing of this code as Apache-2.0 or MIT depending on what best fits within your constraints.

libmerr can be used in the following ways:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// file.c

#include <errno.h>
#include <stdio.h>

#include <merr.h>

enum my_ctx
{
  CTX1 = 1,
};

static const char *
my_ctx_strerror(uint16_t ctx)
{
  switch ((enum my_ctx)ctx) {
  case CTX1:
    return "ctx1";
  }
}

static merr_t
func1(void)
{
  return merr(EINVAL);
}

static merr_t
func2(void)
{
  return merrx(ENOENT, CTX1);
}

static void
caller(void)
{
  char buf[256];

  merr_t err1 = func1();
  merr_strerror(err1, buf, sizeof(buf), NULL);
  fprintf(stderr, "%s\n", buf);
  // file.c:25: Invalid argument (22)

  merr_t err2 = func2();
  merr_strerror(err2, buf, sizeof(buf), NULL);
  fprintf(stderr, "%s\n", buf);
  // file.c:31: No such file or directory (2): ctx1 (1)
}

There are a few other functions in the header file for getting the error, file, line number, and context should you need them. Refer to the repository for more details.

Have a comment or question on one of my posts? Start a discussion in my public inbox by sending an email to ~tristan957/public-inbox@lists.sr.ht. If you are unfamiliar with mailing lists, start with reading about mailing list etiquette. Otherwise just shoot me an email to my address which is linked in the sidebar.

Articles from blogs I follow

What's New in Neovim 0.10

Neovim 0.10 was the longest release cycle since the heady days of the 0.5 release. There are a ton of new features in this release (as well as some breaking changes), so be sure to check the full release notes. You can view the release notes directly in Nvim…

via g.p. anders

Ptyxis on Flathub

You can get Ptyxis on Flathub now if you would like to run the stable version rather than Nightly. Unless you’re interested in helping QA Ptyxis or contributing that is probably the Flatpak you want to have installed. Nightly builds of Ptyxis use the GNOM…

via Happenings in GNOME

Ptyxis on Flathub

You can get Ptyxis on Flathub now if you would like to run the stable version rather than Nightly. Unless you’re interested in helping QA Ptyxis or contributing that is probably the Flatpak you want to have installed. Nightly builds of Ptyxis use the GNOM…

via Happenings in GNOME

Generated by openring