xref: /petsc/src/sys/logging/handler/interface/lhreg.c (revision 19ef957c25d34342168108744150c0ef6ced0e91)
1 
2 #include <petsc/private/petscimpl.h>
3 #include <petsc/private/loghandlerimpl.h>
4 
5 PetscClassId PETSCLOGHANDLER_CLASSID = 0;
6 
7 PetscFunctionList PetscLogHandlerList               = NULL;
8 PetscBool         PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
9 PetscBool         PetscLogHandlerPackageInitialized = PETSC_FALSE;
10 
11 static PetscErrorCode PetscLogHandlerRegisterAll(void)
12 {
13   PetscFunctionBegin;
14   if (PetscLogHandlerRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
15   PetscLogHandlerRegisterAllCalled = PETSC_TRUE;
16   PetscFunctionReturn(PETSC_SUCCESS);
17 }
18 
19 /*@C
20   PetscLogHandlerRegister - Register a new `PetscLogHandler`
21 
22   Not collective
23 
24   Input Parameters:
25 + sname    - The name of a new user-defined creation routine
26 - function - The creation routine
27 
28   Example Usage:
29 .vb
30     PetscLogHandlerRegister("my_profiler", MyPetscLogHandlerCreate);
31 .ve
32 
33   Then, your `PetscLogHandler` type can be chosen with the procedural interface via
34 .vb
35     PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
36     PetscLogHandlerSetType(PetscFE, "my_fe");
37 .ve
38 
39   Level: developer
40 
41 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerSetType()`, `PetscLogHandlerGetType()`
42 @*/
43 PetscErrorCode PetscLogHandlerRegister(const char sname[], PetscErrorCode (*function)(PetscLogHandler))
44 {
45   PetscFunctionBegin;
46   PetscCall(PetscFunctionListAdd(&PetscLogHandlerList, sname, function));
47   PetscFunctionReturn(PETSC_SUCCESS);
48 }
49 
50 /*@C
51   PetscLogHandlerSetType - Set the type of a `PetscLogHandler`
52 
53   Input Parameters:
54 + handler - the `PetscLogHandler`
55 - name    - The kind of log handler
56 
57   Level: developer
58 
59 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerGetType()`
60 @*/
61 PetscErrorCode PetscLogHandlerSetType(PetscLogHandler handler, PetscLogHandlerType name)
62 {
63   PetscErrorCode (*r)(PetscLogHandler);
64   PetscBool match;
65 
66   PetscFunctionBegin;
67   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
68   PetscCall(PetscObjectTypeCompare((PetscObject)handler, name, &match));
69   if (match) PetscFunctionReturn(PETSC_SUCCESS);
70 
71   PetscCall(PetscLogHandlerRegisterAll());
72   PetscCall(PetscFunctionListFind(PetscLogHandlerList, name, &r));
73   PetscCheck(r, PetscObjectComm((PetscObject)handler), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscLogHandler type: %s", name);
74 
75   PetscTryTypeMethod(handler, destroy);
76   handler->ops->destroy = NULL;
77 
78   PetscCall((*r)(handler));
79   PetscCall(PetscObjectChangeTypeName((PetscObject)handler, name));
80   PetscFunctionReturn(PETSC_SUCCESS);
81 }
82 
83 /*@C
84   PetscLogHandlerGetType - Gets the `PetscLoagHandlerType` (as a string) from the `PetscLogHandler` object.
85 
86   Not collective
87 
88   Input Parameter:
89 . handler - the `PetscLogHandler`
90 
91   Output Parameter:
92 . name - The `PetscLogHandlerType` name
93 
94   Level: developer
95 
96 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerCreate()`, `PetscLogHandlerRegister()`, `PetscLogHandlerSetType()`
97 @*/
98 PetscErrorCode PetscLogHandlerGetType(PetscLogHandler handler, PetscLogHandlerType *name)
99 {
100   PetscFunctionBegin;
101   PetscValidHeaderSpecific(handler, PETSCLOGHANDLER_CLASSID, 1);
102   PetscAssertPointer(name, 2);
103   PetscCall(PetscLogHandlerRegisterAll());
104   PetscCall(PetscObjectGetType((PetscObject)handler, name));
105   PetscFunctionReturn(PETSC_SUCCESS);
106 }
107 
108 static PetscErrorCode PetscLogHandlerFinalizePackage(void)
109 {
110   PetscFunctionBegin;
111   PetscCall(PetscFunctionListDestroy(&PetscLogHandlerList));
112   PetscLogHandlerRegisterAllCalled  = PETSC_FALSE;
113   PetscLogHandlerPackageInitialized = PETSC_FALSE;
114   PetscFunctionReturn(PETSC_SUCCESS);
115 }
116 
117 PetscErrorCode PetscLogHandlerPackageInitialize(void)
118 {
119   PetscFunctionBegin;
120   if (PetscLogHandlerPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
121   PetscLogHandlerPackageInitialized = PETSC_TRUE;
122 
123   PetscCall(PetscClassIdRegister("Petsc Log Handler", &PETSCLOGHANDLER_CLASSID));
124   PetscCall(PetscLogHandlerRegisterAll());
125   PetscCall(PetscRegisterFinalize(PetscLogHandlerFinalizePackage));
126   {
127     const PetscClassId classids[] = {PETSCLOGHANDLER_CLASSID};
128 
129     PetscCall(PetscInfoProcessClass("loghandler", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
130   }
131   PetscFunctionReturn(PETSC_SUCCESS);
132 }
133