Linux libjpeg自定义错误处理-can';t覆盖消息输出-ubunu

Linux libjpeg自定义错误处理-can';t覆盖消息输出-ubunu,linux,ubuntu,libjpeg,custom-error-handling,Linux,Ubuntu,Libjpeg,Custom Error Handling,我在Ubuntu上使用libjpeg和straight C,我想覆盖标准的错误处理 我使用的代码声称覆盖了标准错误处理,但它似乎不起作用 我有一个jpeg文件,错误为“jpeg文件过早结束” 如果我试图更改错误消息,它仍然只输出上面的内容 这是我的示例程序 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "jpeglib.h" #include &l

我在Ubuntu上使用libjpeg和straight C,我想覆盖标准的错误处理

我使用的代码声称覆盖了标准错误处理,但它似乎不起作用

我有一个jpeg文件,错误为“jpeg文件过早结束”

如果我试图更改错误消息,它仍然只输出上面的内容

这是我的示例程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "jpeglib.h"

#include <setjmp.h>

struct my_error_mgr {
  struct jpeg_error_mgr pub;    /* "public" fields */

  jmp_buf setjmp_buffer;    /* for return to caller */
};

typedef struct my_error_mgr * my_error_ptr;

/*
 * Here's the routine that will replace the standard error_exit method:
 */

METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  my_error_ptr myerr = (my_error_ptr) cinfo->err;

  /* Always display the message. */
  /* We could postpone this until after returning, if we chose. */
  //  (*cinfo->err->output_message) (cinfo);

  char err_msg[JMSG_LENGTH_MAX];

  (*cinfo->err->format_message) (cinfo, err_msg);
  fprintf(stderr, "My error message: %s", err_msg);

  /* Return control to the setjmp point */
  longjmp(myerr->setjmp_buffer, 1);
}

GLOBAL(int)
read_JPEG_file (char * filename)
{
  struct jpeg_decompress_struct cinfo;
  struct my_error_mgr jerr;
  /* More stuff */
  FILE * infile;        /* source file */
  JSAMPARRAY buffer;        /* Output row buffer */
  int row_stride;       /* physical row width in output buffer */

  if ((infile = fopen(filename, "rb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    return 0;
  }

  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = my_error_exit;

  if (setjmp(jerr.setjmp_buffer)) {
    /* If we get here, the JPEG code has signaled an error.
     * We need to clean up the JPEG object, close the input file, and return.
     */
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
    return 0;
  }

  jpeg_create_decompress(&cinfo);

  jpeg_stdio_src(&cinfo, infile);

  (void) jpeg_read_header(&cinfo, TRUE);

  (void) jpeg_start_decompress(&cinfo);

  row_stride = cinfo.output_width * cinfo.output_components;

  buffer = (*cinfo.mem->alloc_sarray)
        ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

  while (cinfo.output_scanline < cinfo.output_height) {
    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
  }

  (void) jpeg_finish_decompress(&cinfo);

  jpeg_destroy_decompress(&cinfo);

  fclose(infile);

  return 1;
}

int
main() {
  read_JPEG_file("P026451.jpg");
}
#包括
#包括
#包括
#包括“jpeglib.h”
#包括
结构我的错误管理器{
结构jpeg\u错误\u管理发布;/*“公共”字段*/
jmp_buf setjmp_buffer;/*用于返回调用方*/
};
typedef结构我的错误管理器*我的错误ptr;
/*
*以下是将取代标准错误退出方法的例程:
*/
METHODDEF(无效)
我的错误退出(j_common_ptr cinfo)
{
/*cinfo->err实际上指向my_error\u mgr结构,所以强制指针*/
my_error\u ptr myerr=(my_error\u ptr)cinfo->err;
/*始终显示消息*/
/*如果我们愿意的话,我们可以推迟到回国后再做*/
//(*cinfo->err->output_message)(cinfo);
字符err_msg[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo,err_msg);
fprintf(stderr,“我的错误消息:%s”,err_msg);
/*将控件返回到setjmp点*/
longjmp(myerr->setjmp_缓冲区,1);
}
全球(国际)
读取JPEG文件(字符*文件名)
{
结构jpeg\u解压缩\u结构cinfo;
结构我的错误管理器;
/*更多的东西*/
文件*infle;/*源文件*/
JSAMPARRAY缓冲区;/*输出行缓冲区*/
int row_stride;/*输出缓冲区中的物理行宽度*/
if((infle=fopen(文件名,“rb”))==NULL){
fprintf(stderr,“无法打开%s\n”,文件名);
返回0;
}
cinfo.err=jpeg\u std\u error(&jerr.pub);
jerr.pub.error\u exit=my\u error\u exit;
if(setjmp(jerr.setjmp_缓冲区)){
/*如果我们到达这里,JPEG代码已经发出错误信号。
*我们需要清理JPEG对象,关闭输入文件,然后返回。
*/
jpeg\u destroy\u解压缩(&cinfo);
fclose(infle);
返回0;
}
jpeg\u创建\u解压缩(&cinfo);
jpeg_stdio_src(&cinfo,infle);
(void)jpeg_read_头(&cinfo,TRUE);
(无效)jpeg\u开始\u解压缩(&cinfo);
行步距=cinfo.output\u width*cinfo.output\u组件;
缓冲区=(*cinfo.mem->alloc_sarray)
((j_common_ptr)和cinfo,JPOOL_图像,第1行步幅);
while(cinfo.output\u扫描线
我错过了什么。当我运行程序时,它不会输出“我的错误消息:…”

谢谢