Skip to content

同步写日志的功能 #3

Closed
Closed

Description

@sdaereew

char buf[256];
glGetShaderInfoLog(vertexShader, _countof(buf), nullptr, buf);
g_pLog->error("编译失败! {}", std::string(buf));

OpenGL返回的错误信息,目前需要以这种方式写出.用起来不方便,是否能增加一个同步写的功能.

char buf[256];
glGetShaderInfoLog(vertexShader, _countof(buf), nullptr, buf);
g_pLog->error("编译失败! {}", buf);

Activity

  1. pippocao commented on Aug 12, 2024

    @pippocao
    Collaborator

    没明白您的意思。如果您是说同步输出而不是异步输出,请参考
    README.MD 里的
    log.thread_mode

    如果是编译问题,我不知道您是指这个借口要怎样使用会比较方便。毕竟glGetShaderInfoLog这个函数就是这样的。
    如果说代码写法上的优化,主要是char buf[256]这个东西,如果glGetShaderInfoLog会给他一个\0结尾,那可以输出为
    g_pLog->error("编译失败! {}", (char*)buf);
    如果不是\0结尾,可以直接
    g_pLog->error("编译失败! {}", buf);
    这样的话,256的长度就是输出上限

  2. sdaereew commented on Aug 13, 2024

    @sdaereew
    Author

    我是这样理解的.g_pLog->error这个函数是异步执行的,buf这个变量是在栈上的,我这个函数返回后buf里面的数据已经无效了,当g_pLog->error真正写日志的时候会写出垃圾数据.

  3. pippocao commented on Aug 13, 2024

    @pippocao
    Collaborator

    不用担心,不管是同步日志还是异步日志,在你从日志函数返回的时候,所有参数的数据就都已经被拷贝到内部的ring_buffer上了,外面的就随便被回收了。你只要保证在你的error函数执行期间,这个数据没有被回收或者被其他线程修改就行。

    另外,最好不要把char[]转成std::string,这里会生成一次堆上内存分配,直接转char*(如果有\0结尾)或者直接传入char[]就好

  4. sdaereew commented on Aug 13, 2024

    @sdaereew
    Author

    我以为内部只保存了一个char*指针,现在明白了,非常感谢.

  5. self-assigned this
    on Aug 13, 2024
  6. locked and limited conversation to collaborators on Aug 13, 2024
  7. converted this issue into a discussion #4 on Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions