bin: consolidate clients
[m6w6/libmemcached] / src / bin / common / checks.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "options.hpp"
19 #include "libmemcached/common.h"
20
21 #include <fstream>
22
23 bool check_buffering(const client_options &opt, memcached_st &memc) {
24 if (opt.isset("buffer")) {
25 if (MEMCACHED_SUCCESS != memcached_flush_buffers(&memc)) {
26 if (!opt.isset("quiet")) {
27 std::cerr << memcached_last_error_message(&memc) << "\n";
28 }
29 return false;
30 }
31 }
32 return true;
33 }
34
35 bool check_argp(const client_options &opt, char **argp, const char *error_msg) {
36 if (argp && *argp) {
37 return true;
38 }
39 if (!opt.isset("quiet")) {
40 std::cerr << error_msg << "\n";
41 }
42 return false;
43 }
44
45 bool check_memcached(const client_options &opt, memcached_st &memc) {
46 if (!memcached_create(&memc)) {
47 if (!opt.isset("quiet")) {
48 std::cerr << "Failed to initialize memcached client.\n";
49 }
50 return false;
51 }
52 return true;
53 }
54
55 bool check_return(const client_options &opt, memcached_st &memc, const char *key,
56 memcached_return_t rc) {
57 if (!memcached_success(rc)) {
58 if (!opt.isset("quiet")) {
59 if (MEMCACHED_NOTFOUND == rc) {
60 if (opt.isset("verbose")) {
61 std::cerr << "Could not find key '" << key << "'.\n";
62 }
63 } else {
64 std::cerr << "Fatal error for key '" << key
65 << "': " << memcached_last_error_message(&memc) << "\n";
66 }
67 }
68 return false;
69 }
70 return true;
71 }
72
73 std::ostream *check_ostream(const client_options &opt, const char *file, std::ofstream &stream) {
74 if (file && *file) {
75 if (opt.isset("debug")) {
76 std::cerr << "Opening '" << file << "' for writing.\n";
77 }
78 stream.open(file);
79 if (stream.is_open()) {
80 return &stream;
81 } else if (!opt.isset("quiet")) {
82 std::cerr << "Failed to open '" << file << "' for writing.\n";
83 }
84 }
85 return &std::cout;
86 }